OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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 // For linux_syscall_support.h. This makes it safe to call embedded system |
| 6 // calls when in seccomp mode. |
| 7 |
| 8 #include "components/crash/app/breakpad_linux.h" |
| 9 |
| 10 #include <fcntl.h> |
| 11 #include <poll.h> |
| 12 #include <signal.h> |
| 13 #include <stdlib.h> |
| 14 #include <sys/socket.h> |
| 15 #include <sys/time.h> |
| 16 #include <sys/types.h> |
| 17 #include <sys/uio.h> |
| 18 #include <sys/wait.h> |
| 19 #include <time.h> |
| 20 #include <unistd.h> |
| 21 |
| 22 #include <algorithm> |
| 23 #include <string> |
| 24 |
| 25 #include "base/base_switches.h" |
| 26 #include "base/command_line.h" |
| 27 #include "base/debug/crash_logging.h" |
| 28 #include "base/debug/dump_without_crashing.h" |
| 29 #include "base/files/file_path.h" |
| 30 #include "base/linux_util.h" |
| 31 #include "base/path_service.h" |
| 32 #include "base/posix/eintr_wrapper.h" |
| 33 #include "base/posix/global_descriptors.h" |
| 34 #include "base/process/memory.h" |
| 35 #include "base/strings/string_util.h" |
| 36 #include "breakpad/src/client/linux/crash_generation/crash_generation_client.h" |
| 37 #include "breakpad/src/client/linux/handler/exception_handler.h" |
| 38 #include "breakpad/src/client/linux/minidump_writer/directory_reader.h" |
| 39 #include "breakpad/src/common/linux/linux_libc_support.h" |
| 40 #include "breakpad/src/common/memory.h" |
| 41 #include "build/build_config.h" |
| 42 #include "components/crash/app/breakpad_linux_impl.h" |
| 43 #include "components/crash/app/crash_reporter_client.h" |
| 44 #include "content/public/common/content_descriptors.h" |
| 45 |
| 46 #if defined(OS_ANDROID) |
| 47 #include <android/log.h> |
| 48 #include <sys/stat.h> |
| 49 |
| 50 #include "base/android/build_info.h" |
| 51 #include "base/android/path_utils.h" |
| 52 #include "base/debug/leak_annotations.h" |
| 53 #endif |
| 54 #include "third_party/lss/linux_syscall_support.h" |
| 55 |
| 56 #if defined(ADDRESS_SANITIZER) |
| 57 #include <ucontext.h> // for getcontext(). |
| 58 #endif |
| 59 |
| 60 #if defined(OS_ANDROID) |
| 61 #define STAT_STRUCT struct stat |
| 62 #define FSTAT_FUNC fstat |
| 63 #else |
| 64 #define STAT_STRUCT struct kernel_stat |
| 65 #define FSTAT_FUNC sys_fstat |
| 66 #endif |
| 67 |
| 68 // Some versions of gcc are prone to warn about unused return values. In cases |
| 69 // where we either a) know the call cannot fail, or b) there is nothing we |
| 70 // can do when a call fails, we mark the return code as ignored. This avoids |
| 71 // spurious compiler warnings. |
| 72 #define IGNORE_RET(x) do { if (x); } while (0) |
| 73 |
| 74 using crash_reporter::GetCrashReporterClient; |
| 75 using google_breakpad::ExceptionHandler; |
| 76 using google_breakpad::MinidumpDescriptor; |
| 77 |
| 78 namespace breakpad { |
| 79 |
| 80 namespace { |
| 81 |
| 82 #if !defined(OS_CHROMEOS) |
| 83 const char kUploadURL[] = "https://clients2.google.com/cr/report"; |
| 84 #endif |
| 85 |
| 86 bool g_is_crash_reporter_enabled = false; |
| 87 uint64_t g_process_start_time = 0; |
| 88 pid_t g_pid = 0; |
| 89 char* g_crash_log_path = nullptr; |
| 90 ExceptionHandler* g_breakpad = nullptr; |
| 91 |
| 92 #if defined(ADDRESS_SANITIZER) |
| 93 const char* g_asan_report_str = nullptr; |
| 94 #endif |
| 95 #if defined(OS_ANDROID) |
| 96 char* g_process_type = nullptr; |
| 97 ExceptionHandler* g_microdump = nullptr; |
| 98 const char* g_microdump_build_fingerprint = nullptr; |
| 99 const char* g_microdump_product_info = nullptr; |
| 100 #endif |
| 101 |
| 102 CrashKeyStorage* g_crash_keys = nullptr; |
| 103 |
| 104 // Writes the value |v| as 16 hex characters to the memory pointed at by |
| 105 // |output|. |
| 106 void write_uint64_hex(char* output, uint64_t v) { |
| 107 static const char hextable[] = "0123456789abcdef"; |
| 108 |
| 109 for (int i = 15; i >= 0; --i) { |
| 110 output[i] = hextable[v & 15]; |
| 111 v >>= 4; |
| 112 } |
| 113 } |
| 114 |
| 115 // The following helper functions are for calculating uptime. |
| 116 |
| 117 // Converts a struct timeval to milliseconds. |
| 118 uint64_t timeval_to_ms(struct timeval *tv) { |
| 119 uint64_t ret = tv->tv_sec; // Avoid overflow by explicitly using a uint64_t. |
| 120 ret *= 1000; |
| 121 ret += tv->tv_usec / 1000; |
| 122 return ret; |
| 123 } |
| 124 |
| 125 // Converts a struct timeval to milliseconds. |
| 126 uint64_t kernel_timeval_to_ms(struct kernel_timeval *tv) { |
| 127 uint64_t ret = tv->tv_sec; // Avoid overflow by explicitly using a uint64_t. |
| 128 ret *= 1000; |
| 129 ret += tv->tv_usec / 1000; |
| 130 return ret; |
| 131 } |
| 132 |
| 133 // String buffer size to use to convert a uint64_t to string. |
| 134 const size_t kUint64StringSize = 21; |
| 135 |
| 136 void SetProcessStartTime() { |
| 137 // Set the base process start time value. |
| 138 struct timeval tv; |
| 139 if (!gettimeofday(&tv, nullptr)) |
| 140 g_process_start_time = timeval_to_ms(&tv); |
| 141 else |
| 142 g_process_start_time = 0; |
| 143 } |
| 144 |
| 145 // uint64_t version of my_int_len() from |
| 146 // breakpad/src/common/linux/linux_libc_support.h. Return the length of the |
| 147 // given, non-negative integer when expressed in base 10. |
| 148 unsigned my_uint64_len(uint64_t i) { |
| 149 if (!i) |
| 150 return 1; |
| 151 |
| 152 unsigned len = 0; |
| 153 while (i) { |
| 154 len++; |
| 155 i /= 10; |
| 156 } |
| 157 |
| 158 return len; |
| 159 } |
| 160 |
| 161 // uint64_t version of my_uitos() from |
| 162 // breakpad/src/common/linux/linux_libc_support.h. Convert a non-negative |
| 163 // integer to a string (not null-terminated). |
| 164 void my_uint64tos(char* output, uint64_t i, unsigned i_len) { |
| 165 for (unsigned index = i_len; index; --index, i /= 10) |
| 166 output[index - 1] = '0' + (i % 10); |
| 167 } |
| 168 |
| 169 #if !defined(OS_CHROMEOS) |
| 170 bool my_isxdigit(char c) { |
| 171 return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'f'); |
| 172 } |
| 173 #endif |
| 174 |
| 175 size_t LengthWithoutTrailingSpaces(const char* str, size_t len) { |
| 176 while (len > 0 && str[len - 1] == ' ') { |
| 177 len--; |
| 178 } |
| 179 return len; |
| 180 } |
| 181 |
| 182 void SetClientIdFromCommandLine(const base::CommandLine& command_line) { |
| 183 // Get the guid from the command line switch. |
| 184 std::string switch_value = |
| 185 command_line.GetSwitchValueASCII(switches::kEnableCrashReporter); |
| 186 GetCrashReporterClient()->SetCrashReporterClientIdFromGUID(switch_value); |
| 187 } |
| 188 |
| 189 // MIME substrings. |
| 190 #if defined(OS_CHROMEOS) |
| 191 const char g_sep[] = ":"; |
| 192 #endif |
| 193 const char g_rn[] = "\r\n"; |
| 194 const char g_form_data_msg[] = "Content-Disposition: form-data; name=\""; |
| 195 const char g_quote_msg[] = "\""; |
| 196 const char g_dashdash_msg[] = "--"; |
| 197 const char g_dump_msg[] = "upload_file_minidump\"; filename=\"dump\""; |
| 198 #if defined(ADDRESS_SANITIZER) |
| 199 const char g_log_msg[] = "upload_file_log\"; filename=\"log\""; |
| 200 #endif |
| 201 const char g_content_type_msg[] = "Content-Type: application/octet-stream"; |
| 202 |
| 203 // MimeWriter manages an iovec for writing MIMEs to a file. |
| 204 class MimeWriter { |
| 205 public: |
| 206 static const int kIovCapacity = 30; |
| 207 static const size_t kMaxCrashChunkSize = 64; |
| 208 |
| 209 MimeWriter(int fd, const char* const mime_boundary); |
| 210 ~MimeWriter(); |
| 211 |
| 212 // Append boundary. |
| 213 virtual void AddBoundary(); |
| 214 |
| 215 // Append end of file boundary. |
| 216 virtual void AddEnd(); |
| 217 |
| 218 // Append key/value pair with specified sizes. |
| 219 virtual void AddPairData(const char* msg_type, |
| 220 size_t msg_type_size, |
| 221 const char* msg_data, |
| 222 size_t msg_data_size); |
| 223 |
| 224 // Append key/value pair. |
| 225 void AddPairString(const char* msg_type, |
| 226 const char* msg_data) { |
| 227 AddPairData(msg_type, my_strlen(msg_type), msg_data, my_strlen(msg_data)); |
| 228 } |
| 229 |
| 230 // Append key/value pair, splitting value into chunks no larger than |
| 231 // |chunk_size|. |chunk_size| cannot be greater than |kMaxCrashChunkSize|. |
| 232 // The msg_type string will have a counter suffix to distinguish each chunk. |
| 233 virtual void AddPairDataInChunks(const char* msg_type, |
| 234 size_t msg_type_size, |
| 235 const char* msg_data, |
| 236 size_t msg_data_size, |
| 237 size_t chunk_size, |
| 238 bool strip_trailing_spaces); |
| 239 |
| 240 // Add binary file contents to be uploaded with the specified filename. |
| 241 virtual void AddFileContents(const char* filename_msg, |
| 242 uint8_t* file_data, |
| 243 size_t file_size); |
| 244 |
| 245 // Flush any pending iovecs to the output file. |
| 246 void Flush() { |
| 247 IGNORE_RET(sys_writev(fd_, iov_, iov_index_)); |
| 248 iov_index_ = 0; |
| 249 } |
| 250 |
| 251 protected: |
| 252 void AddItem(const void* base, size_t size); |
| 253 // Minor performance trade-off for easier-to-maintain code. |
| 254 void AddString(const char* str) { |
| 255 AddItem(str, my_strlen(str)); |
| 256 } |
| 257 void AddItemWithoutTrailingSpaces(const void* base, size_t size); |
| 258 |
| 259 struct kernel_iovec iov_[kIovCapacity]; |
| 260 int iov_index_; |
| 261 |
| 262 // Output file descriptor. |
| 263 int fd_; |
| 264 |
| 265 const char* const mime_boundary_; |
| 266 |
| 267 private: |
| 268 DISALLOW_COPY_AND_ASSIGN(MimeWriter); |
| 269 }; |
| 270 |
| 271 MimeWriter::MimeWriter(int fd, const char* const mime_boundary) |
| 272 : iov_index_(0), |
| 273 fd_(fd), |
| 274 mime_boundary_(mime_boundary) { |
| 275 } |
| 276 |
| 277 MimeWriter::~MimeWriter() { |
| 278 } |
| 279 |
| 280 void MimeWriter::AddBoundary() { |
| 281 AddString(mime_boundary_); |
| 282 AddString(g_rn); |
| 283 } |
| 284 |
| 285 void MimeWriter::AddEnd() { |
| 286 AddString(mime_boundary_); |
| 287 AddString(g_dashdash_msg); |
| 288 AddString(g_rn); |
| 289 } |
| 290 |
| 291 void MimeWriter::AddPairData(const char* msg_type, |
| 292 size_t msg_type_size, |
| 293 const char* msg_data, |
| 294 size_t msg_data_size) { |
| 295 AddString(g_form_data_msg); |
| 296 AddItem(msg_type, msg_type_size); |
| 297 AddString(g_quote_msg); |
| 298 AddString(g_rn); |
| 299 AddString(g_rn); |
| 300 AddItem(msg_data, msg_data_size); |
| 301 AddString(g_rn); |
| 302 } |
| 303 |
| 304 void MimeWriter::AddPairDataInChunks(const char* msg_type, |
| 305 size_t msg_type_size, |
| 306 const char* msg_data, |
| 307 size_t msg_data_size, |
| 308 size_t chunk_size, |
| 309 bool strip_trailing_spaces) { |
| 310 if (chunk_size > kMaxCrashChunkSize) |
| 311 return; |
| 312 |
| 313 unsigned i = 0; |
| 314 size_t done = 0, msg_length = msg_data_size; |
| 315 |
| 316 while (msg_length) { |
| 317 char num[kUint64StringSize]; |
| 318 const unsigned num_len = my_uint_len(++i); |
| 319 my_uitos(num, i, num_len); |
| 320 |
| 321 size_t chunk_len = std::min(chunk_size, msg_length); |
| 322 |
| 323 AddString(g_form_data_msg); |
| 324 AddItem(msg_type, msg_type_size); |
| 325 AddItem(num, num_len); |
| 326 AddString(g_quote_msg); |
| 327 AddString(g_rn); |
| 328 AddString(g_rn); |
| 329 if (strip_trailing_spaces) { |
| 330 AddItemWithoutTrailingSpaces(msg_data + done, chunk_len); |
| 331 } else { |
| 332 AddItem(msg_data + done, chunk_len); |
| 333 } |
| 334 AddString(g_rn); |
| 335 AddBoundary(); |
| 336 Flush(); |
| 337 |
| 338 done += chunk_len; |
| 339 msg_length -= chunk_len; |
| 340 } |
| 341 } |
| 342 |
| 343 void MimeWriter::AddFileContents(const char* filename_msg, uint8_t* file_data, |
| 344 size_t file_size) { |
| 345 AddString(g_form_data_msg); |
| 346 AddString(filename_msg); |
| 347 AddString(g_rn); |
| 348 AddString(g_content_type_msg); |
| 349 AddString(g_rn); |
| 350 AddString(g_rn); |
| 351 AddItem(file_data, file_size); |
| 352 AddString(g_rn); |
| 353 } |
| 354 |
| 355 void MimeWriter::AddItem(const void* base, size_t size) { |
| 356 // Check if the iovec is full and needs to be flushed to output file. |
| 357 if (iov_index_ == kIovCapacity) { |
| 358 Flush(); |
| 359 } |
| 360 iov_[iov_index_].iov_base = const_cast<void*>(base); |
| 361 iov_[iov_index_].iov_len = size; |
| 362 ++iov_index_; |
| 363 } |
| 364 |
| 365 void MimeWriter::AddItemWithoutTrailingSpaces(const void* base, size_t size) { |
| 366 AddItem(base, LengthWithoutTrailingSpaces(static_cast<const char*>(base), |
| 367 size)); |
| 368 } |
| 369 |
| 370 #if defined(OS_CHROMEOS) |
| 371 // This subclass is used on Chromium OS to report crashes in a format easy for |
| 372 // the central crash reporting facility to understand. |
| 373 // Format is <name>:<data length in decimal>:<data> |
| 374 class CrashReporterWriter : public MimeWriter { |
| 375 public: |
| 376 explicit CrashReporterWriter(int fd); |
| 377 |
| 378 void AddBoundary() override; |
| 379 |
| 380 void AddEnd() override; |
| 381 |
| 382 void AddPairData(const char* msg_type, |
| 383 size_t msg_type_size, |
| 384 const char* msg_data, |
| 385 size_t msg_data_size) override; |
| 386 |
| 387 void AddPairDataInChunks(const char* msg_type, |
| 388 size_t msg_type_size, |
| 389 const char* msg_data, |
| 390 size_t msg_data_size, |
| 391 size_t chunk_size, |
| 392 bool strip_trailing_spaces) override; |
| 393 |
| 394 void AddFileContents(const char* filename_msg, |
| 395 uint8_t* file_data, |
| 396 size_t file_size) override; |
| 397 |
| 398 private: |
| 399 DISALLOW_COPY_AND_ASSIGN(CrashReporterWriter); |
| 400 }; |
| 401 |
| 402 |
| 403 CrashReporterWriter::CrashReporterWriter(int fd) : MimeWriter(fd, "") {} |
| 404 |
| 405 // No-ops. |
| 406 void CrashReporterWriter::AddBoundary() {} |
| 407 void CrashReporterWriter::AddEnd() {} |
| 408 |
| 409 void CrashReporterWriter::AddPairData(const char* msg_type, |
| 410 size_t msg_type_size, |
| 411 const char* msg_data, |
| 412 size_t msg_data_size) { |
| 413 char data[kUint64StringSize]; |
| 414 const unsigned data_len = my_uint_len(msg_data_size); |
| 415 my_uitos(data, msg_data_size, data_len); |
| 416 |
| 417 AddItem(msg_type, msg_type_size); |
| 418 AddString(g_sep); |
| 419 AddItem(data, data_len); |
| 420 AddString(g_sep); |
| 421 AddItem(msg_data, msg_data_size); |
| 422 Flush(); |
| 423 } |
| 424 |
| 425 void CrashReporterWriter::AddPairDataInChunks(const char* msg_type, |
| 426 size_t msg_type_size, |
| 427 const char* msg_data, |
| 428 size_t msg_data_size, |
| 429 size_t chunk_size, |
| 430 bool strip_trailing_spaces) { |
| 431 if (chunk_size > kMaxCrashChunkSize) |
| 432 return; |
| 433 |
| 434 unsigned i = 0; |
| 435 size_t done = 0; |
| 436 size_t msg_length = msg_data_size; |
| 437 |
| 438 while (msg_length) { |
| 439 char num[kUint64StringSize]; |
| 440 const unsigned num_len = my_uint_len(++i); |
| 441 my_uitos(num, i, num_len); |
| 442 |
| 443 size_t chunk_len = std::min(chunk_size, msg_length); |
| 444 |
| 445 size_t write_len = chunk_len; |
| 446 if (strip_trailing_spaces) { |
| 447 // Take care of this here because we need to know the exact length of |
| 448 // what is going to be written. |
| 449 write_len = LengthWithoutTrailingSpaces(msg_data + done, write_len); |
| 450 } |
| 451 |
| 452 char data[kUint64StringSize]; |
| 453 const unsigned data_len = my_uint_len(write_len); |
| 454 my_uitos(data, write_len, data_len); |
| 455 |
| 456 AddItem(msg_type, msg_type_size); |
| 457 AddItem(num, num_len); |
| 458 AddString(g_sep); |
| 459 AddItem(data, data_len); |
| 460 AddString(g_sep); |
| 461 AddItem(msg_data + done, write_len); |
| 462 Flush(); |
| 463 |
| 464 done += chunk_len; |
| 465 msg_length -= chunk_len; |
| 466 } |
| 467 } |
| 468 |
| 469 void CrashReporterWriter::AddFileContents(const char* filename_msg, |
| 470 uint8_t* file_data, |
| 471 size_t file_size) { |
| 472 char data[kUint64StringSize]; |
| 473 const unsigned data_len = my_uint_len(file_size); |
| 474 my_uitos(data, file_size, data_len); |
| 475 |
| 476 AddString(filename_msg); |
| 477 AddString(g_sep); |
| 478 AddItem(data, data_len); |
| 479 AddString(g_sep); |
| 480 AddItem(file_data, file_size); |
| 481 Flush(); |
| 482 } |
| 483 #endif // defined(OS_CHROMEOS) |
| 484 |
| 485 void DumpProcess() { |
| 486 if (g_breakpad) |
| 487 g_breakpad->WriteMinidump(); |
| 488 |
| 489 #if defined(OS_ANDROID) |
| 490 // If microdumps are enabled write also a microdump on the system log. |
| 491 if (g_microdump) |
| 492 g_microdump->WriteMinidump(); |
| 493 #endif |
| 494 } |
| 495 |
| 496 #if defined(OS_ANDROID) |
| 497 const char kGoogleBreakpad[] = "google-breakpad"; |
| 498 #endif |
| 499 |
| 500 size_t WriteLog(const char* buf, size_t nbytes) { |
| 501 #if defined(OS_ANDROID) |
| 502 return __android_log_write(ANDROID_LOG_WARN, kGoogleBreakpad, buf); |
| 503 #else |
| 504 return sys_write(2, buf, nbytes); |
| 505 #endif |
| 506 } |
| 507 |
| 508 size_t WriteNewline() { |
| 509 return WriteLog("\n", 1); |
| 510 } |
| 511 |
| 512 #if defined(OS_ANDROID) |
| 513 void AndroidLogWriteHorizontalRule() { |
| 514 __android_log_write(ANDROID_LOG_WARN, kGoogleBreakpad, |
| 515 "### ### ### ### ### ### ### ### ### ### ### ### ###"); |
| 516 } |
| 517 |
| 518 // Android's native crash handler outputs a diagnostic tombstone to the device |
| 519 // log. By returning false from the HandlerCallbacks, breakpad will reinstall |
| 520 // the previous (i.e. native) signal handlers before returning from its own |
| 521 // handler. A Chrome build fingerprint is written to the log, so that the |
| 522 // specific build of Chrome and the location of the archived Chrome symbols can |
| 523 // be determined directly from it. |
| 524 bool FinalizeCrashDoneAndroid(bool is_browser_process) { |
| 525 base::android::BuildInfo* android_build_info = |
| 526 base::android::BuildInfo::GetInstance(); |
| 527 |
| 528 AndroidLogWriteHorizontalRule(); |
| 529 __android_log_write(ANDROID_LOG_WARN, kGoogleBreakpad, |
| 530 "Chrome build fingerprint:"); |
| 531 __android_log_write(ANDROID_LOG_WARN, kGoogleBreakpad, |
| 532 android_build_info->package_version_name()); |
| 533 __android_log_write(ANDROID_LOG_WARN, kGoogleBreakpad, |
| 534 android_build_info->package_version_code()); |
| 535 __android_log_write(ANDROID_LOG_WARN, kGoogleBreakpad, |
| 536 CHROME_BUILD_ID); |
| 537 AndroidLogWriteHorizontalRule(); |
| 538 |
| 539 if (!is_browser_process && |
| 540 android_build_info->sdk_int() >= 18 && |
| 541 my_strcmp(android_build_info->build_type(), "eng") != 0 && |
| 542 my_strcmp(android_build_info->build_type(), "userdebug") != 0) { |
| 543 // On JB MR2 and later, the system crash handler displays a dialog. For |
| 544 // renderer crashes, this is a bad user experience and so this is disabled |
| 545 // for user builds of Android. |
| 546 // TODO(cjhopman): There should be some way to recover the crash stack from |
| 547 // non-uploading user clients. See http://crbug.com/273706. |
| 548 __android_log_write(ANDROID_LOG_WARN, |
| 549 kGoogleBreakpad, |
| 550 "Tombstones are disabled on JB MR2+ user builds."); |
| 551 AndroidLogWriteHorizontalRule(); |
| 552 return true; |
| 553 } |
| 554 return false; |
| 555 } |
| 556 #endif |
| 557 |
| 558 bool CrashDone(const MinidumpDescriptor& minidump, |
| 559 const bool upload, |
| 560 const bool succeeded) { |
| 561 // WARNING: this code runs in a compromised context. It may not call into |
| 562 // libc nor allocate memory normally. |
| 563 if (!succeeded) { |
| 564 const char msg[] = "Failed to generate minidump."; |
| 565 WriteLog(msg, sizeof(msg) - 1); |
| 566 return false; |
| 567 } |
| 568 |
| 569 DCHECK(!minidump.IsFD()); |
| 570 |
| 571 BreakpadInfo info = {0}; |
| 572 info.filename = minidump.path(); |
| 573 info.fd = minidump.fd(); |
| 574 #if defined(ADDRESS_SANITIZER) |
| 575 google_breakpad::PageAllocator allocator; |
| 576 const size_t log_path_len = my_strlen(minidump.path()); |
| 577 char* log_path = reinterpret_cast<char*>(allocator.Alloc(log_path_len + 1)); |
| 578 my_memcpy(log_path, minidump.path(), log_path_len); |
| 579 my_memcpy(log_path + log_path_len - 4, ".log", 4); |
| 580 log_path[log_path_len] = '\0'; |
| 581 info.log_filename = log_path; |
| 582 #endif |
| 583 info.process_type = "browser"; |
| 584 info.process_type_length = 7; |
| 585 info.distro = base::g_linux_distro; |
| 586 info.distro_length = my_strlen(base::g_linux_distro); |
| 587 info.upload = upload; |
| 588 info.process_start_time = g_process_start_time; |
| 589 info.oom_size = base::g_oom_size; |
| 590 info.pid = g_pid; |
| 591 info.crash_keys = g_crash_keys; |
| 592 HandleCrashDump(info); |
| 593 #if defined(OS_ANDROID) |
| 594 return FinalizeCrashDoneAndroid(true /* is_browser_process */); |
| 595 #else |
| 596 return true; |
| 597 #endif |
| 598 } |
| 599 |
| 600 // Wrapper function, do not add more code here. |
| 601 bool CrashDoneNoUpload(const MinidumpDescriptor& minidump, |
| 602 void* context, |
| 603 bool succeeded) { |
| 604 return CrashDone(minidump, false, succeeded); |
| 605 } |
| 606 |
| 607 #if !defined(OS_ANDROID) |
| 608 // Wrapper function, do not add more code here. |
| 609 bool CrashDoneUpload(const MinidumpDescriptor& minidump, |
| 610 void* context, |
| 611 bool succeeded) { |
| 612 return CrashDone(minidump, true, succeeded); |
| 613 } |
| 614 #endif |
| 615 |
| 616 #if defined(ADDRESS_SANITIZER) |
| 617 extern "C" |
| 618 void __asan_set_error_report_callback(void (*cb)(const char*)); |
| 619 |
| 620 extern "C" |
| 621 void AsanLinuxBreakpadCallback(const char* report) { |
| 622 g_asan_report_str = report; |
| 623 // Send minidump here. |
| 624 g_breakpad->SimulateSignalDelivery(SIGKILL); |
| 625 } |
| 626 #endif |
| 627 |
| 628 void EnableCrashDumping(bool unattended) { |
| 629 g_is_crash_reporter_enabled = true; |
| 630 |
| 631 base::FilePath tmp_path("/tmp"); |
| 632 PathService::Get(base::DIR_TEMP, &tmp_path); |
| 633 |
| 634 base::FilePath dumps_path(tmp_path); |
| 635 if (GetCrashReporterClient()->GetCrashDumpLocation(&dumps_path)) { |
| 636 base::FilePath logfile = |
| 637 dumps_path.Append(GetCrashReporterClient()->GetReporterLogFilename()); |
| 638 std::string logfile_str = logfile.value(); |
| 639 const size_t crash_log_path_len = logfile_str.size() + 1; |
| 640 g_crash_log_path = new char[crash_log_path_len]; |
| 641 strncpy(g_crash_log_path, logfile_str.c_str(), crash_log_path_len); |
| 642 } |
| 643 DCHECK(!g_breakpad); |
| 644 MinidumpDescriptor minidump_descriptor(dumps_path.value()); |
| 645 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 646 switches::kFullMemoryCrashReport)) { |
| 647 minidump_descriptor.set_size_limit(-1); // unlimited. |
| 648 } else { |
| 649 minidump_descriptor.set_size_limit(kMaxMinidumpFileSize); |
| 650 } |
| 651 #if defined(OS_ANDROID) |
| 652 unattended = true; // Android never uploads directly. |
| 653 #endif |
| 654 if (unattended) { |
| 655 g_breakpad = new ExceptionHandler( |
| 656 minidump_descriptor, |
| 657 nullptr, |
| 658 CrashDoneNoUpload, |
| 659 nullptr, |
| 660 true, // Install handlers. |
| 661 -1); // Server file descriptor. -1 for in-process. |
| 662 return; |
| 663 } |
| 664 |
| 665 #if !defined(OS_ANDROID) |
| 666 // Attended mode |
| 667 g_breakpad = new ExceptionHandler( |
| 668 minidump_descriptor, |
| 669 nullptr, |
| 670 CrashDoneUpload, |
| 671 nullptr, |
| 672 true, // Install handlers. |
| 673 -1); // Server file descriptor. -1 for in-process. |
| 674 #endif |
| 675 } |
| 676 |
| 677 #if defined(OS_ANDROID) |
| 678 bool MicrodumpCrashDone(const MinidumpDescriptor& minidump, |
| 679 void* context, |
| 680 bool succeeded) { |
| 681 // WARNING: this code runs in a compromised context. It may not call into |
| 682 // libc nor allocate memory normally. |
| 683 if (!succeeded) { |
| 684 static const char msg[] = "Microdump crash handler failed.\n"; |
| 685 WriteLog(msg, sizeof(msg) - 1); |
| 686 return false; |
| 687 } |
| 688 |
| 689 const bool is_browser_process = (context != nullptr); |
| 690 return FinalizeCrashDoneAndroid(is_browser_process); |
| 691 } |
| 692 |
| 693 // The microdump handler does NOT upload anything. It just dumps out on the |
| 694 // system console (logcat) a restricted and serialized variant of a minidump. |
| 695 // See crbug.com/410294 for more details. |
| 696 void InitMicrodumpCrashHandlerIfNecessary(const std::string& process_type) { |
| 697 if (!GetCrashReporterClient()->ShouldEnableBreakpadMicrodumps()) |
| 698 return; |
| 699 |
| 700 VLOG(1) << "Enabling microdumps crash handler (process_type:" |
| 701 << process_type << ")"; |
| 702 |
| 703 // The exception handler runs in a compromised context and cannot use c_str() |
| 704 // as that would require the heap. Therefore, we have to guarantee that the |
| 705 // build fingerprint and product info pointers are always valid. |
| 706 const char* product_name = nullptr; |
| 707 const char* product_version = nullptr; |
| 708 GetCrashReporterClient()->GetProductNameAndVersion(&product_name, |
| 709 &product_version); |
| 710 |
| 711 MinidumpDescriptor descriptor(MinidumpDescriptor::kMicrodumpOnConsole); |
| 712 |
| 713 if (product_name && product_version) { |
| 714 g_microdump_product_info = strdup( |
| 715 (product_name + std::string(":") + product_version).c_str()); |
| 716 ANNOTATE_LEAKING_OBJECT_PTR(g_microdump_product_info); |
| 717 descriptor.SetMicrodumpProductInfo(g_microdump_product_info); |
| 718 } |
| 719 |
| 720 const char* android_build_fp = |
| 721 base::android::BuildInfo::GetInstance()->android_build_fp(); |
| 722 if (android_build_fp) { |
| 723 g_microdump_build_fingerprint = strdup(android_build_fp); |
| 724 ANNOTATE_LEAKING_OBJECT_PTR(g_microdump_build_fingerprint); |
| 725 descriptor.SetMicrodumpBuildFingerprint(g_microdump_build_fingerprint); |
| 726 } |
| 727 |
| 728 DCHECK(!g_microdump); |
| 729 bool is_browser_process = process_type.empty() || process_type == "webview"; |
| 730 g_microdump = new ExceptionHandler( |
| 731 descriptor, |
| 732 nullptr, |
| 733 MicrodumpCrashDone, |
| 734 reinterpret_cast<void*>(is_browser_process), |
| 735 true, // Install handlers. |
| 736 -1); // Server file descriptor. -1 for in-process. |
| 737 return; |
| 738 } |
| 739 |
| 740 bool CrashDoneInProcessNoUpload( |
| 741 const google_breakpad::MinidumpDescriptor& descriptor, |
| 742 void* context, |
| 743 const bool succeeded) { |
| 744 // WARNING: this code runs in a compromised context. It may not call into |
| 745 // libc nor allocate memory normally. |
| 746 if (!succeeded) { |
| 747 static const char msg[] = "Crash dump generation failed.\n"; |
| 748 WriteLog(msg, sizeof(msg) - 1); |
| 749 return false; |
| 750 } |
| 751 |
| 752 // Start constructing the message to send to the browser. |
| 753 BreakpadInfo info = {0}; |
| 754 info.filename = nullptr; |
| 755 info.fd = descriptor.fd(); |
| 756 info.process_type = g_process_type; |
| 757 info.process_type_length = my_strlen(g_process_type); |
| 758 info.distro = nullptr; |
| 759 info.distro_length = 0; |
| 760 info.upload = false; |
| 761 info.process_start_time = g_process_start_time; |
| 762 info.pid = g_pid; |
| 763 info.crash_keys = g_crash_keys; |
| 764 HandleCrashDump(info); |
| 765 return FinalizeCrashDoneAndroid(false /* is_browser_process */); |
| 766 } |
| 767 |
| 768 void EnableNonBrowserCrashDumping(const std::string& process_type, |
| 769 int minidump_fd) { |
| 770 // This will guarantee that the BuildInfo has been initialized and subsequent |
| 771 // calls will not require memory allocation. |
| 772 base::android::BuildInfo::GetInstance(); |
| 773 SetClientIdFromCommandLine(*base::CommandLine::ForCurrentProcess()); |
| 774 |
| 775 // On Android, the current sandboxing uses process isolation, in which the |
| 776 // child process runs with a different UID. That breaks the normal crash |
| 777 // reporting where the browser process generates the minidump by inspecting |
| 778 // the child process. This is because the browser process now does not have |
| 779 // the permission to access the states of the child process (as it has a |
| 780 // different UID). |
| 781 // TODO(jcivelli): http://b/issue?id=6776356 we should use a watchdog |
| 782 // process forked from the renderer process that generates the minidump. |
| 783 if (minidump_fd == -1) { |
| 784 LOG(ERROR) << "Minidump file descriptor not found, crash reporting will " |
| 785 " not work."; |
| 786 return; |
| 787 } |
| 788 SetProcessStartTime(); |
| 789 g_pid = getpid(); |
| 790 |
| 791 g_is_crash_reporter_enabled = true; |
| 792 // Save the process type (it is leaked). |
| 793 const size_t process_type_len = process_type.size() + 1; |
| 794 g_process_type = new char[process_type_len]; |
| 795 strncpy(g_process_type, process_type.c_str(), process_type_len); |
| 796 new google_breakpad::ExceptionHandler(MinidumpDescriptor(minidump_fd), |
| 797 nullptr, CrashDoneInProcessNoUpload, nullptr, true, -1); |
| 798 } |
| 799 #else |
| 800 // Non-Browser = Extension, Gpu, Plugins, Ppapi and Renderer |
| 801 class NonBrowserCrashHandler : public google_breakpad::CrashGenerationClient { |
| 802 public: |
| 803 NonBrowserCrashHandler() |
| 804 : server_fd_(base::GlobalDescriptors::GetInstance()->Get( |
| 805 kCrashDumpSignal)) { |
| 806 } |
| 807 |
| 808 ~NonBrowserCrashHandler() override {} |
| 809 |
| 810 bool RequestDump(const void* crash_context, |
| 811 size_t crash_context_size) override { |
| 812 int fds[2] = { -1, -1 }; |
| 813 if (sys_socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) { |
| 814 static const char msg[] = "Failed to create socket for crash dumping.\n"; |
| 815 WriteLog(msg, sizeof(msg) - 1); |
| 816 return false; |
| 817 } |
| 818 |
| 819 // Start constructing the message to send to the browser. |
| 820 char b; // Dummy variable for sys_read below. |
| 821 const char* b_addr = &b; // Get the address of |b| so we can create the |
| 822 // expected /proc/[pid]/syscall content in the |
| 823 // browser to convert namespace tids. |
| 824 |
| 825 // The length of the control message: |
| 826 static const unsigned kControlMsgSize = sizeof(int); |
| 827 static const unsigned kControlMsgSpaceSize = CMSG_SPACE(kControlMsgSize); |
| 828 static const unsigned kControlMsgLenSize = CMSG_LEN(kControlMsgSize); |
| 829 |
| 830 struct kernel_msghdr msg; |
| 831 my_memset(&msg, 0, sizeof(struct kernel_msghdr)); |
| 832 struct kernel_iovec iov[kCrashIovSize]; |
| 833 iov[0].iov_base = const_cast<void*>(crash_context); |
| 834 iov[0].iov_len = crash_context_size; |
| 835 iov[1].iov_base = &b_addr; |
| 836 iov[1].iov_len = sizeof(b_addr); |
| 837 iov[2].iov_base = &fds[0]; |
| 838 iov[2].iov_len = sizeof(fds[0]); |
| 839 iov[3].iov_base = &g_process_start_time; |
| 840 iov[3].iov_len = sizeof(g_process_start_time); |
| 841 iov[4].iov_base = &base::g_oom_size; |
| 842 iov[4].iov_len = sizeof(base::g_oom_size); |
| 843 google_breakpad::SerializedNonAllocatingMap* serialized_map; |
| 844 iov[5].iov_len = g_crash_keys->Serialize( |
| 845 const_cast<const google_breakpad::SerializedNonAllocatingMap**>( |
| 846 &serialized_map)); |
| 847 iov[5].iov_base = serialized_map; |
| 848 #if !defined(ADDRESS_SANITIZER) |
| 849 static_assert(5 == kCrashIovSize - 1, "kCrashIovSize should equal 6"); |
| 850 #else |
| 851 iov[6].iov_base = const_cast<char*>(g_asan_report_str); |
| 852 iov[6].iov_len = kMaxAsanReportSize + 1; |
| 853 static_assert(6 == kCrashIovSize - 1, "kCrashIovSize should equal 7"); |
| 854 #endif |
| 855 |
| 856 msg.msg_iov = iov; |
| 857 msg.msg_iovlen = kCrashIovSize; |
| 858 char cmsg[kControlMsgSpaceSize]; |
| 859 my_memset(cmsg, 0, kControlMsgSpaceSize); |
| 860 msg.msg_control = cmsg; |
| 861 msg.msg_controllen = sizeof(cmsg); |
| 862 |
| 863 struct cmsghdr *hdr = CMSG_FIRSTHDR(&msg); |
| 864 hdr->cmsg_level = SOL_SOCKET; |
| 865 hdr->cmsg_type = SCM_RIGHTS; |
| 866 hdr->cmsg_len = kControlMsgLenSize; |
| 867 ((int*)CMSG_DATA(hdr))[0] = fds[1]; |
| 868 |
| 869 if (HANDLE_EINTR(sys_sendmsg(server_fd_, &msg, 0)) < 0) { |
| 870 static const char errmsg[] = "Failed to tell parent about crash.\n"; |
| 871 WriteLog(errmsg, sizeof(errmsg) - 1); |
| 872 IGNORE_RET(sys_close(fds[0])); |
| 873 IGNORE_RET(sys_close(fds[1])); |
| 874 return false; |
| 875 } |
| 876 IGNORE_RET(sys_close(fds[1])); |
| 877 |
| 878 if (HANDLE_EINTR(sys_read(fds[0], &b, 1)) != 1) { |
| 879 static const char errmsg[] = "Parent failed to complete crash dump.\n"; |
| 880 WriteLog(errmsg, sizeof(errmsg) - 1); |
| 881 } |
| 882 IGNORE_RET(sys_close(fds[0])); |
| 883 |
| 884 return true; |
| 885 } |
| 886 |
| 887 private: |
| 888 // The pipe FD to the browser process, which will handle the crash dumping. |
| 889 const int server_fd_; |
| 890 |
| 891 DISALLOW_COPY_AND_ASSIGN(NonBrowserCrashHandler); |
| 892 }; |
| 893 |
| 894 void EnableNonBrowserCrashDumping() { |
| 895 g_is_crash_reporter_enabled = true; |
| 896 // We deliberately leak this object. |
| 897 DCHECK(!g_breakpad); |
| 898 |
| 899 g_breakpad = new ExceptionHandler( |
| 900 MinidumpDescriptor("/tmp"), // Unused but needed or Breakpad will assert. |
| 901 nullptr, |
| 902 nullptr, |
| 903 nullptr, |
| 904 true, |
| 905 -1); |
| 906 g_breakpad->set_crash_generation_client(new NonBrowserCrashHandler()); |
| 907 } |
| 908 #endif // defined(OS_ANDROID) |
| 909 |
| 910 void SetCrashKeyValue(const base::StringPiece& key, |
| 911 const base::StringPiece& value) { |
| 912 g_crash_keys->SetKeyValue(key.data(), value.data()); |
| 913 } |
| 914 |
| 915 void ClearCrashKey(const base::StringPiece& key) { |
| 916 g_crash_keys->RemoveKey(key.data()); |
| 917 } |
| 918 |
| 919 // GetCrashReporterClient() cannot call any Set methods until after |
| 920 // InitCrashKeys(). |
| 921 void InitCrashKeys() { |
| 922 g_crash_keys = new CrashKeyStorage; |
| 923 GetCrashReporterClient()->RegisterCrashKeys(); |
| 924 base::debug::SetCrashKeyReportingFunctions(&SetCrashKeyValue, &ClearCrashKey); |
| 925 } |
| 926 |
| 927 // Miscellaneous initialization functions to call after Breakpad has been |
| 928 // enabled. |
| 929 void PostEnableBreakpadInitialization() { |
| 930 SetProcessStartTime(); |
| 931 g_pid = getpid(); |
| 932 |
| 933 base::debug::SetDumpWithoutCrashingFunction(&DumpProcess); |
| 934 #if defined(ADDRESS_SANITIZER) |
| 935 // Register the callback for AddressSanitizer error reporting. |
| 936 __asan_set_error_report_callback(AsanLinuxBreakpadCallback); |
| 937 #endif |
| 938 } |
| 939 |
| 940 } // namespace |
| 941 |
| 942 void LoadDataFromFD(google_breakpad::PageAllocator& allocator, |
| 943 int fd, bool close_fd, uint8_t** file_data, size_t* size) { |
| 944 STAT_STRUCT st; |
| 945 if (FSTAT_FUNC(fd, &st) != 0) { |
| 946 static const char msg[] = "Cannot upload crash dump: stat failed\n"; |
| 947 WriteLog(msg, sizeof(msg) - 1); |
| 948 if (close_fd) |
| 949 IGNORE_RET(sys_close(fd)); |
| 950 return; |
| 951 } |
| 952 |
| 953 *file_data = reinterpret_cast<uint8_t*>(allocator.Alloc(st.st_size)); |
| 954 if (!(*file_data)) { |
| 955 static const char msg[] = "Cannot upload crash dump: cannot alloc\n"; |
| 956 WriteLog(msg, sizeof(msg) - 1); |
| 957 if (close_fd) |
| 958 IGNORE_RET(sys_close(fd)); |
| 959 return; |
| 960 } |
| 961 my_memset(*file_data, 0xf, st.st_size); |
| 962 |
| 963 *size = st.st_size; |
| 964 int byte_read = sys_read(fd, *file_data, *size); |
| 965 if (byte_read == -1) { |
| 966 static const char msg[] = "Cannot upload crash dump: read failed\n"; |
| 967 WriteLog(msg, sizeof(msg) - 1); |
| 968 if (close_fd) |
| 969 IGNORE_RET(sys_close(fd)); |
| 970 return; |
| 971 } |
| 972 |
| 973 if (close_fd) |
| 974 IGNORE_RET(sys_close(fd)); |
| 975 } |
| 976 |
| 977 void LoadDataFromFile(google_breakpad::PageAllocator& allocator, |
| 978 const char* filename, |
| 979 int* fd, uint8_t** file_data, size_t* size) { |
| 980 // WARNING: this code runs in a compromised context. It may not call into |
| 981 // libc nor allocate memory normally. |
| 982 *fd = sys_open(filename, O_RDONLY, 0); |
| 983 *size = 0; |
| 984 |
| 985 if (*fd < 0) { |
| 986 static const char msg[] = "Cannot upload crash dump: failed to open\n"; |
| 987 WriteLog(msg, sizeof(msg) - 1); |
| 988 return; |
| 989 } |
| 990 |
| 991 LoadDataFromFD(allocator, *fd, true, file_data, size); |
| 992 } |
| 993 |
| 994 // Spawn the appropriate upload process for the current OS: |
| 995 // - generic Linux invokes wget. |
| 996 // - ChromeOS invokes crash_reporter. |
| 997 // |dumpfile| is the path to the dump data file. |
| 998 // |mime_boundary| is only used on Linux. |
| 999 // |exe_buf| is only used on CrOS and is the crashing process' name. |
| 1000 void ExecUploadProcessOrTerminate(const BreakpadInfo& info, |
| 1001 const char* dumpfile, |
| 1002 const char* mime_boundary, |
| 1003 const char* exe_buf, |
| 1004 google_breakpad::PageAllocator* allocator) { |
| 1005 #if defined(OS_CHROMEOS) |
| 1006 // CrOS uses crash_reporter instead of wget to report crashes, |
| 1007 // it needs to know where the crash dump lives and the pid and uid of the |
| 1008 // crashing process. |
| 1009 static const char kCrashReporterBinary[] = "/sbin/crash_reporter"; |
| 1010 |
| 1011 char pid_buf[kUint64StringSize]; |
| 1012 uint64_t pid_str_length = my_uint64_len(info.pid); |
| 1013 my_uint64tos(pid_buf, info.pid, pid_str_length); |
| 1014 pid_buf[pid_str_length] = '\0'; |
| 1015 |
| 1016 char uid_buf[kUint64StringSize]; |
| 1017 uid_t uid = geteuid(); |
| 1018 uint64_t uid_str_length = my_uint64_len(uid); |
| 1019 my_uint64tos(uid_buf, uid, uid_str_length); |
| 1020 uid_buf[uid_str_length] = '\0'; |
| 1021 |
| 1022 const char kChromeFlag[] = "--chrome="; |
| 1023 size_t buf_len = my_strlen(dumpfile) + sizeof(kChromeFlag); |
| 1024 char* chrome_flag = reinterpret_cast<char*>(allocator->Alloc(buf_len)); |
| 1025 chrome_flag[0] = '\0'; |
| 1026 my_strlcat(chrome_flag, kChromeFlag, buf_len); |
| 1027 my_strlcat(chrome_flag, dumpfile, buf_len); |
| 1028 |
| 1029 const char kPidFlag[] = "--pid="; |
| 1030 buf_len = my_strlen(pid_buf) + sizeof(kPidFlag); |
| 1031 char* pid_flag = reinterpret_cast<char*>(allocator->Alloc(buf_len)); |
| 1032 pid_flag[0] = '\0'; |
| 1033 my_strlcat(pid_flag, kPidFlag, buf_len); |
| 1034 my_strlcat(pid_flag, pid_buf, buf_len); |
| 1035 |
| 1036 const char kUidFlag[] = "--uid="; |
| 1037 buf_len = my_strlen(uid_buf) + sizeof(kUidFlag); |
| 1038 char* uid_flag = reinterpret_cast<char*>(allocator->Alloc(buf_len)); |
| 1039 uid_flag[0] = '\0'; |
| 1040 my_strlcat(uid_flag, kUidFlag, buf_len); |
| 1041 my_strlcat(uid_flag, uid_buf, buf_len); |
| 1042 |
| 1043 const char kExeBuf[] = "--exe="; |
| 1044 buf_len = my_strlen(exe_buf) + sizeof(kExeBuf); |
| 1045 char* exe_flag = reinterpret_cast<char*>(allocator->Alloc(buf_len)); |
| 1046 exe_flag[0] = '\0'; |
| 1047 my_strlcat(exe_flag, kExeBuf, buf_len); |
| 1048 my_strlcat(exe_flag, exe_buf, buf_len); |
| 1049 |
| 1050 const char* args[] = { |
| 1051 kCrashReporterBinary, |
| 1052 chrome_flag, |
| 1053 pid_flag, |
| 1054 uid_flag, |
| 1055 exe_flag, |
| 1056 nullptr, |
| 1057 }; |
| 1058 static const char msg[] = "Cannot upload crash dump: cannot exec " |
| 1059 "/sbin/crash_reporter\n"; |
| 1060 #else |
| 1061 // Compress |dumpfile| with gzip. |
| 1062 const pid_t gzip_child = sys_fork(); |
| 1063 if (gzip_child < 0) { |
| 1064 static const char msg[] = "sys_fork() for gzip process failed.\n"; |
| 1065 WriteLog(msg, sizeof(msg) - 1); |
| 1066 sys__exit(1); |
| 1067 } |
| 1068 if (!gzip_child) { |
| 1069 // gzip process. |
| 1070 const char* args[] = { |
| 1071 "/bin/gzip", |
| 1072 "-f", // Do not prompt to verify before overwriting. |
| 1073 dumpfile, |
| 1074 nullptr, |
| 1075 }; |
| 1076 execve(args[0], const_cast<char**>(args), environ); |
| 1077 static const char msg[] = "Cannot exec gzip.\n"; |
| 1078 WriteLog(msg, sizeof(msg) - 1); |
| 1079 sys__exit(1); |
| 1080 } |
| 1081 // Wait for gzip process. |
| 1082 int status = 0; |
| 1083 if (sys_waitpid(gzip_child, &status, 0) != gzip_child || |
| 1084 !WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 1085 static const char msg[] = "sys_waitpid() for gzip process failed.\n"; |
| 1086 WriteLog(msg, sizeof(msg) - 1); |
| 1087 sys_kill(gzip_child, SIGKILL); |
| 1088 sys__exit(1); |
| 1089 } |
| 1090 |
| 1091 static const char kGzipExtension[] = ".gz"; |
| 1092 const size_t gzip_file_size = my_strlen(dumpfile) + sizeof(kGzipExtension); |
| 1093 char* const gzip_file = reinterpret_cast<char*>(allocator->Alloc( |
| 1094 gzip_file_size)); |
| 1095 my_strlcpy(gzip_file, dumpfile, gzip_file_size); |
| 1096 my_strlcat(gzip_file, kGzipExtension, gzip_file_size); |
| 1097 |
| 1098 // Rename |gzip_file| to |dumpfile| (the original file was deleted by gzip). |
| 1099 if (rename(gzip_file, dumpfile)) { |
| 1100 static const char msg[] = "Failed to rename gzipped file.\n"; |
| 1101 WriteLog(msg, sizeof(msg) - 1); |
| 1102 sys__exit(1); |
| 1103 } |
| 1104 |
| 1105 // The --header argument to wget looks like: |
| 1106 // --header=Content-Encoding: gzip |
| 1107 // --header=Content-Type: multipart/form-data; boundary=XYZ |
| 1108 // where the boundary has two fewer leading '-' chars |
| 1109 static const char header_content_encoding[] = |
| 1110 "--header=Content-Encoding: gzip"; |
| 1111 static const char header_msg[] = |
| 1112 "--header=Content-Type: multipart/form-data; boundary="; |
| 1113 const size_t header_content_type_size = |
| 1114 sizeof(header_msg) - 1 + my_strlen(mime_boundary) - 2 + 1; |
| 1115 char* const header_content_type = reinterpret_cast<char*>(allocator->Alloc( |
| 1116 header_content_type_size)); |
| 1117 my_strlcpy(header_content_type, header_msg, header_content_type_size); |
| 1118 my_strlcat(header_content_type, mime_boundary + 2, header_content_type_size); |
| 1119 |
| 1120 // The --post-file argument to wget looks like: |
| 1121 // --post-file=/tmp/... |
| 1122 static const char post_file_msg[] = "--post-file="; |
| 1123 const size_t post_file_size = |
| 1124 sizeof(post_file_msg) - 1 + my_strlen(dumpfile) + 1; |
| 1125 char* const post_file = reinterpret_cast<char*>(allocator->Alloc( |
| 1126 post_file_size)); |
| 1127 my_strlcpy(post_file, post_file_msg, post_file_size); |
| 1128 my_strlcat(post_file, dumpfile, post_file_size); |
| 1129 |
| 1130 static const char kWgetBinary[] = "/usr/bin/wget"; |
| 1131 const char* args[] = { |
| 1132 kWgetBinary, |
| 1133 header_content_encoding, |
| 1134 header_content_type, |
| 1135 post_file, |
| 1136 kUploadURL, |
| 1137 "--timeout=10", // Set a timeout so we don't hang forever. |
| 1138 "--tries=1", // Don't retry if the upload fails. |
| 1139 "-O", // output reply to fd 3 |
| 1140 "/dev/fd/3", |
| 1141 nullptr, |
| 1142 }; |
| 1143 static const char msg[] = "Cannot upload crash dump: cannot exec " |
| 1144 "/usr/bin/wget\n"; |
| 1145 #endif |
| 1146 execve(args[0], const_cast<char**>(args), environ); |
| 1147 WriteLog(msg, sizeof(msg) - 1); |
| 1148 sys__exit(1); |
| 1149 } |
| 1150 |
| 1151 // Runs in the helper process to wait for the upload process running |
| 1152 // ExecUploadProcessOrTerminate() to finish. Returns the number of bytes written |
| 1153 // to |fd| and save the written contents to |buf|. |
| 1154 // |buf| needs to be big enough to hold |bytes_to_read| + 1 characters. |
| 1155 size_t WaitForCrashReportUploadProcess(int fd, size_t bytes_to_read, |
| 1156 char* buf) { |
| 1157 size_t bytes_read = 0; |
| 1158 |
| 1159 // Upload should finish in about 10 seconds. Add a few more 500 ms |
| 1160 // internals to account for process startup time. |
| 1161 for (size_t wait_count = 0; wait_count < 24; ++wait_count) { |
| 1162 struct kernel_pollfd poll_fd; |
| 1163 poll_fd.fd = fd; |
| 1164 poll_fd.events = POLLIN | POLLPRI | POLLERR; |
| 1165 int ret = sys_poll(&poll_fd, 1, 500); |
| 1166 if (ret < 0) { |
| 1167 // Error |
| 1168 break; |
| 1169 } else if (ret > 0) { |
| 1170 // There is data to read. |
| 1171 ssize_t len = HANDLE_EINTR( |
| 1172 sys_read(fd, buf + bytes_read, bytes_to_read - bytes_read)); |
| 1173 if (len < 0) |
| 1174 break; |
| 1175 bytes_read += len; |
| 1176 if (bytes_read == bytes_to_read) |
| 1177 break; |
| 1178 } |
| 1179 // |ret| == 0 -> timed out, continue waiting. |
| 1180 // or |bytes_read| < |bytes_to_read| still, keep reading. |
| 1181 } |
| 1182 buf[bytes_to_read] = 0; // Always NUL terminate the buffer. |
| 1183 return bytes_read; |
| 1184 } |
| 1185 |
| 1186 // |buf| should be |expected_len| + 1 characters in size and nullptr terminated. |
| 1187 bool IsValidCrashReportId(const char* buf, size_t bytes_read, |
| 1188 size_t expected_len) { |
| 1189 if (bytes_read != expected_len) |
| 1190 return false; |
| 1191 #if defined(OS_CHROMEOS) |
| 1192 return my_strcmp(buf, "_sys_cr_finished") == 0; |
| 1193 #else |
| 1194 for (size_t i = 0; i < bytes_read; ++i) { |
| 1195 if (!my_isxdigit(buf[i])) |
| 1196 return false; |
| 1197 } |
| 1198 return true; |
| 1199 #endif |
| 1200 } |
| 1201 |
| 1202 // |buf| should be |expected_len| + 1 characters in size and nullptr terminated. |
| 1203 void HandleCrashReportId(const char* buf, size_t bytes_read, |
| 1204 size_t expected_len) { |
| 1205 WriteNewline(); |
| 1206 if (!IsValidCrashReportId(buf, bytes_read, expected_len)) { |
| 1207 #if defined(OS_CHROMEOS) |
| 1208 static const char msg[] = |
| 1209 "System crash-reporter failed to process crash report."; |
| 1210 #else |
| 1211 static const char msg[] = "Failed to get crash dump id."; |
| 1212 #endif |
| 1213 WriteLog(msg, sizeof(msg) - 1); |
| 1214 WriteNewline(); |
| 1215 |
| 1216 static const char id_msg[] = "Report Id: "; |
| 1217 WriteLog(id_msg, sizeof(id_msg) - 1); |
| 1218 WriteLog(buf, bytes_read); |
| 1219 WriteNewline(); |
| 1220 return; |
| 1221 } |
| 1222 |
| 1223 #if defined(OS_CHROMEOS) |
| 1224 static const char msg[] = "Crash dump received by crash_reporter\n"; |
| 1225 WriteLog(msg, sizeof(msg) - 1); |
| 1226 #else |
| 1227 // Write crash dump id to stderr. |
| 1228 static const char msg[] = "Crash dump id: "; |
| 1229 WriteLog(msg, sizeof(msg) - 1); |
| 1230 WriteLog(buf, my_strlen(buf)); |
| 1231 WriteNewline(); |
| 1232 |
| 1233 // Write crash dump id to crash log as: seconds_since_epoch,crash_id |
| 1234 struct kernel_timeval tv; |
| 1235 if (g_crash_log_path && !sys_gettimeofday(&tv, nullptr)) { |
| 1236 uint64_t time = kernel_timeval_to_ms(&tv) / 1000; |
| 1237 char time_str[kUint64StringSize]; |
| 1238 const unsigned time_len = my_uint64_len(time); |
| 1239 my_uint64tos(time_str, time, time_len); |
| 1240 |
| 1241 const int kLogOpenFlags = O_CREAT | O_WRONLY | O_APPEND | O_CLOEXEC; |
| 1242 int log_fd = sys_open(g_crash_log_path, kLogOpenFlags, 0600); |
| 1243 if (log_fd > 0) { |
| 1244 sys_write(log_fd, time_str, time_len); |
| 1245 sys_write(log_fd, ",", 1); |
| 1246 sys_write(log_fd, buf, my_strlen(buf)); |
| 1247 sys_write(log_fd, "\n", 1); |
| 1248 IGNORE_RET(sys_close(log_fd)); |
| 1249 } |
| 1250 } |
| 1251 #endif |
| 1252 } |
| 1253 |
| 1254 #if defined(OS_CHROMEOS) |
| 1255 const char* GetCrashingProcessName(const BreakpadInfo& info, |
| 1256 google_breakpad::PageAllocator* allocator) { |
| 1257 // Symlink to process binary is at /proc/###/exe. |
| 1258 char linkpath[kUint64StringSize + sizeof("/proc/") + sizeof("/exe")] = |
| 1259 "/proc/"; |
| 1260 uint64_t pid_value_len = my_uint64_len(info.pid); |
| 1261 my_uint64tos(linkpath + sizeof("/proc/") - 1, info.pid, pid_value_len); |
| 1262 linkpath[sizeof("/proc/") - 1 + pid_value_len] = '\0'; |
| 1263 my_strlcat(linkpath, "/exe", sizeof(linkpath)); |
| 1264 |
| 1265 const int kMaxSize = 4096; |
| 1266 char* link = reinterpret_cast<char*>(allocator->Alloc(kMaxSize)); |
| 1267 if (link) { |
| 1268 ssize_t size = readlink(linkpath, link, kMaxSize); |
| 1269 if (size < kMaxSize && size > 0) { |
| 1270 // readlink(2) doesn't add a terminating NUL, so do it now. |
| 1271 link[size] = '\0'; |
| 1272 |
| 1273 const char* name = my_strrchr(link, '/'); |
| 1274 if (name) |
| 1275 return name + 1; |
| 1276 return link; |
| 1277 } |
| 1278 } |
| 1279 // Either way too long, or a read error. |
| 1280 return "chrome-crash-unknown-process"; |
| 1281 } |
| 1282 #endif |
| 1283 |
| 1284 void HandleCrashDump(const BreakpadInfo& info) { |
| 1285 int dumpfd; |
| 1286 bool keep_fd = false; |
| 1287 size_t dump_size; |
| 1288 uint8_t* dump_data; |
| 1289 google_breakpad::PageAllocator allocator; |
| 1290 const char* exe_buf = nullptr; |
| 1291 |
| 1292 if (GetCrashReporterClient()->HandleCrashDump(info.filename)) { |
| 1293 return; |
| 1294 } |
| 1295 |
| 1296 #if defined(OS_CHROMEOS) |
| 1297 // Grab the crashing process' name now, when it should still be available. |
| 1298 // If we try to do this later in our grandchild the crashing process has |
| 1299 // already terminated. |
| 1300 exe_buf = GetCrashingProcessName(info, &allocator); |
| 1301 #endif |
| 1302 |
| 1303 if (info.fd != -1) { |
| 1304 // Dump is provided with an open FD. |
| 1305 keep_fd = true; |
| 1306 dumpfd = info.fd; |
| 1307 |
| 1308 // The FD is pointing to the end of the file. |
| 1309 // Rewind, we'll read the data next. |
| 1310 if (lseek(dumpfd, 0, SEEK_SET) == -1) { |
| 1311 static const char msg[] = "Cannot upload crash dump: failed to " |
| 1312 "reposition minidump FD\n"; |
| 1313 WriteLog(msg, sizeof(msg) - 1); |
| 1314 IGNORE_RET(sys_close(dumpfd)); |
| 1315 return; |
| 1316 } |
| 1317 LoadDataFromFD(allocator, info.fd, false, &dump_data, &dump_size); |
| 1318 } else { |
| 1319 // Dump is provided with a path. |
| 1320 keep_fd = false; |
| 1321 LoadDataFromFile(allocator, info.filename, &dumpfd, &dump_data, &dump_size); |
| 1322 } |
| 1323 |
| 1324 // TODO(jcivelli): make log work when using FDs. |
| 1325 #if defined(ADDRESS_SANITIZER) |
| 1326 int logfd; |
| 1327 size_t log_size; |
| 1328 uint8_t* log_data; |
| 1329 // Load the AddressSanitizer log into log_data. |
| 1330 LoadDataFromFile(allocator, info.log_filename, &logfd, &log_data, &log_size); |
| 1331 #endif |
| 1332 |
| 1333 // We need to build a MIME block for uploading to the server. Since we are |
| 1334 // going to fork and run wget, it needs to be written to a temp file. |
| 1335 const int ufd = sys_open("/dev/urandom", O_RDONLY, 0); |
| 1336 if (ufd < 0) { |
| 1337 static const char msg[] = "Cannot upload crash dump because /dev/urandom" |
| 1338 " is missing\n"; |
| 1339 WriteLog(msg, sizeof(msg) - 1); |
| 1340 return; |
| 1341 } |
| 1342 |
| 1343 static const char temp_file_template[] = |
| 1344 "/tmp/chromium-upload-XXXXXXXXXXXXXXXX"; |
| 1345 char temp_file[sizeof(temp_file_template)]; |
| 1346 int temp_file_fd = -1; |
| 1347 if (keep_fd) { |
| 1348 temp_file_fd = dumpfd; |
| 1349 // Rewind the destination, we are going to overwrite it. |
| 1350 if (lseek(dumpfd, 0, SEEK_SET) == -1) { |
| 1351 static const char msg[] = "Cannot upload crash dump: failed to " |
| 1352 "reposition minidump FD (2)\n"; |
| 1353 WriteLog(msg, sizeof(msg) - 1); |
| 1354 IGNORE_RET(sys_close(dumpfd)); |
| 1355 return; |
| 1356 } |
| 1357 } else { |
| 1358 if (info.upload) { |
| 1359 my_memcpy(temp_file, temp_file_template, sizeof(temp_file_template)); |
| 1360 |
| 1361 for (unsigned i = 0; i < 10; ++i) { |
| 1362 uint64_t t; |
| 1363 sys_read(ufd, &t, sizeof(t)); |
| 1364 write_uint64_hex(temp_file + sizeof(temp_file) - (16 + 1), t); |
| 1365 |
| 1366 temp_file_fd = sys_open(temp_file, O_WRONLY | O_CREAT | O_EXCL, 0600); |
| 1367 if (temp_file_fd >= 0) |
| 1368 break; |
| 1369 } |
| 1370 |
| 1371 if (temp_file_fd < 0) { |
| 1372 static const char msg[] = "Failed to create temporary file in /tmp: " |
| 1373 "cannot upload crash dump\n"; |
| 1374 WriteLog(msg, sizeof(msg) - 1); |
| 1375 IGNORE_RET(sys_close(ufd)); |
| 1376 return; |
| 1377 } |
| 1378 } else { |
| 1379 temp_file_fd = sys_open(info.filename, O_WRONLY, 0600); |
| 1380 if (temp_file_fd < 0) { |
| 1381 static const char msg[] = "Failed to save crash dump: failed to open\n"; |
| 1382 WriteLog(msg, sizeof(msg) - 1); |
| 1383 IGNORE_RET(sys_close(ufd)); |
| 1384 return; |
| 1385 } |
| 1386 } |
| 1387 } |
| 1388 |
| 1389 // The MIME boundary is 28 hyphens, followed by a 64-bit nonce and a NUL. |
| 1390 char mime_boundary[28 + 16 + 1]; |
| 1391 my_memset(mime_boundary, '-', 28); |
| 1392 uint64_t boundary_rand; |
| 1393 sys_read(ufd, &boundary_rand, sizeof(boundary_rand)); |
| 1394 write_uint64_hex(mime_boundary + 28, boundary_rand); |
| 1395 mime_boundary[28 + 16] = 0; |
| 1396 IGNORE_RET(sys_close(ufd)); |
| 1397 |
| 1398 // The MIME block looks like this: |
| 1399 // BOUNDARY \r\n |
| 1400 // Content-Disposition: form-data; name="prod" \r\n \r\n |
| 1401 // Chrome_Linux \r\n |
| 1402 // BOUNDARY \r\n |
| 1403 // Content-Disposition: form-data; name="ver" \r\n \r\n |
| 1404 // 1.2.3.4 \r\n |
| 1405 // BOUNDARY \r\n |
| 1406 // |
| 1407 // zero or one: |
| 1408 // Content-Disposition: form-data; name="ptime" \r\n \r\n |
| 1409 // abcdef \r\n |
| 1410 // BOUNDARY \r\n |
| 1411 // |
| 1412 // zero or one: |
| 1413 // Content-Disposition: form-data; name="ptype" \r\n \r\n |
| 1414 // abcdef \r\n |
| 1415 // BOUNDARY \r\n |
| 1416 // |
| 1417 // zero or one: |
| 1418 // Content-Disposition: form-data; name="lsb-release" \r\n \r\n |
| 1419 // abcdef \r\n |
| 1420 // BOUNDARY \r\n |
| 1421 // |
| 1422 // zero or one: |
| 1423 // Content-Disposition: form-data; name="oom-size" \r\n \r\n |
| 1424 // 1234567890 \r\n |
| 1425 // BOUNDARY \r\n |
| 1426 // |
| 1427 // zero or more (up to CrashKeyStorage::num_entries = 64): |
| 1428 // Content-Disposition: form-data; name=crash-key-name \r\n |
| 1429 // crash-key-value \r\n |
| 1430 // BOUNDARY \r\n |
| 1431 // |
| 1432 // Content-Disposition: form-data; name="dump"; filename="dump" \r\n |
| 1433 // Content-Type: application/octet-stream \r\n \r\n |
| 1434 // <dump contents> |
| 1435 // \r\n BOUNDARY -- \r\n |
| 1436 |
| 1437 #if defined(OS_CHROMEOS) |
| 1438 CrashReporterWriter writer(temp_file_fd); |
| 1439 #else |
| 1440 MimeWriter writer(temp_file_fd, mime_boundary); |
| 1441 #endif |
| 1442 { |
| 1443 const char* product_name = ""; |
| 1444 const char* version = ""; |
| 1445 |
| 1446 GetCrashReporterClient()->GetProductNameAndVersion(&product_name, &version); |
| 1447 |
| 1448 writer.AddBoundary(); |
| 1449 writer.AddPairString("prod", product_name); |
| 1450 writer.AddBoundary(); |
| 1451 writer.AddPairString("ver", version); |
| 1452 writer.AddBoundary(); |
| 1453 if (info.pid > 0) { |
| 1454 char pid_value_buf[kUint64StringSize]; |
| 1455 uint64_t pid_value_len = my_uint64_len(info.pid); |
| 1456 my_uint64tos(pid_value_buf, info.pid, pid_value_len); |
| 1457 static const char pid_key_name[] = "pid"; |
| 1458 writer.AddPairData(pid_key_name, sizeof(pid_key_name) - 1, |
| 1459 pid_value_buf, pid_value_len); |
| 1460 writer.AddBoundary(); |
| 1461 } |
| 1462 #if defined(OS_ANDROID) |
| 1463 // Addtional MIME blocks are added for logging on Android devices. |
| 1464 static const char android_build_id[] = "android_build_id"; |
| 1465 static const char android_build_fp[] = "android_build_fp"; |
| 1466 static const char device[] = "device"; |
| 1467 static const char model[] = "model"; |
| 1468 static const char brand[] = "brand"; |
| 1469 static const char exception_info[] = "exception_info"; |
| 1470 |
| 1471 base::android::BuildInfo* android_build_info = |
| 1472 base::android::BuildInfo::GetInstance(); |
| 1473 writer.AddPairString( |
| 1474 android_build_id, android_build_info->android_build_id()); |
| 1475 writer.AddBoundary(); |
| 1476 writer.AddPairString( |
| 1477 android_build_fp, android_build_info->android_build_fp()); |
| 1478 writer.AddBoundary(); |
| 1479 writer.AddPairString(device, android_build_info->device()); |
| 1480 writer.AddBoundary(); |
| 1481 writer.AddPairString(model, android_build_info->model()); |
| 1482 writer.AddBoundary(); |
| 1483 writer.AddPairString(brand, android_build_info->brand()); |
| 1484 writer.AddBoundary(); |
| 1485 if (android_build_info->java_exception_info() != nullptr) { |
| 1486 writer.AddPairString(exception_info, |
| 1487 android_build_info->java_exception_info()); |
| 1488 writer.AddBoundary(); |
| 1489 } |
| 1490 #endif |
| 1491 writer.Flush(); |
| 1492 } |
| 1493 |
| 1494 if (info.process_start_time > 0) { |
| 1495 struct kernel_timeval tv; |
| 1496 if (!sys_gettimeofday(&tv, nullptr)) { |
| 1497 uint64_t time = kernel_timeval_to_ms(&tv); |
| 1498 if (time > info.process_start_time) { |
| 1499 time -= info.process_start_time; |
| 1500 char time_str[kUint64StringSize]; |
| 1501 const unsigned time_len = my_uint64_len(time); |
| 1502 my_uint64tos(time_str, time, time_len); |
| 1503 |
| 1504 static const char process_time_msg[] = "ptime"; |
| 1505 writer.AddPairData(process_time_msg, sizeof(process_time_msg) - 1, |
| 1506 time_str, time_len); |
| 1507 writer.AddBoundary(); |
| 1508 writer.Flush(); |
| 1509 } |
| 1510 } |
| 1511 } |
| 1512 |
| 1513 if (info.process_type_length) { |
| 1514 writer.AddPairString("ptype", info.process_type); |
| 1515 writer.AddBoundary(); |
| 1516 writer.Flush(); |
| 1517 } |
| 1518 |
| 1519 if (info.distro_length) { |
| 1520 static const char distro_msg[] = "lsb-release"; |
| 1521 writer.AddPairString(distro_msg, info.distro); |
| 1522 writer.AddBoundary(); |
| 1523 writer.Flush(); |
| 1524 } |
| 1525 |
| 1526 if (info.oom_size) { |
| 1527 char oom_size_str[kUint64StringSize]; |
| 1528 const unsigned oom_size_len = my_uint64_len(info.oom_size); |
| 1529 my_uint64tos(oom_size_str, info.oom_size, oom_size_len); |
| 1530 static const char oom_size_msg[] = "oom-size"; |
| 1531 writer.AddPairData(oom_size_msg, sizeof(oom_size_msg) - 1, |
| 1532 oom_size_str, oom_size_len); |
| 1533 writer.AddBoundary(); |
| 1534 writer.Flush(); |
| 1535 } |
| 1536 |
| 1537 if (info.crash_keys) { |
| 1538 CrashKeyStorage::Iterator crash_key_iterator(*info.crash_keys); |
| 1539 const CrashKeyStorage::Entry* entry; |
| 1540 while ((entry = crash_key_iterator.Next())) { |
| 1541 writer.AddPairString(entry->key, entry->value); |
| 1542 writer.AddBoundary(); |
| 1543 writer.Flush(); |
| 1544 } |
| 1545 } |
| 1546 |
| 1547 writer.AddFileContents(g_dump_msg, dump_data, dump_size); |
| 1548 #if defined(ADDRESS_SANITIZER) |
| 1549 // Append a multipart boundary and the contents of the AddressSanitizer log. |
| 1550 writer.AddBoundary(); |
| 1551 writer.AddFileContents(g_log_msg, log_data, log_size); |
| 1552 #endif |
| 1553 writer.AddEnd(); |
| 1554 writer.Flush(); |
| 1555 |
| 1556 IGNORE_RET(sys_close(temp_file_fd)); |
| 1557 |
| 1558 #if defined(OS_ANDROID) |
| 1559 if (info.filename) { |
| 1560 size_t filename_length = my_strlen(info.filename); |
| 1561 |
| 1562 // If this was a file, we need to copy it to the right place and use the |
| 1563 // right file name so it gets uploaded by the browser. |
| 1564 const char msg[] = "Output crash dump file:"; |
| 1565 WriteLog(msg, sizeof(msg) - 1); |
| 1566 WriteLog(info.filename, filename_length); |
| 1567 |
| 1568 char pid_buf[kUint64StringSize]; |
| 1569 size_t pid_str_length = my_uint64_len(info.pid); |
| 1570 my_uint64tos(pid_buf, info.pid, pid_str_length); |
| 1571 pid_buf[pid_str_length] = 0; // my_uint64tos() doesn't null-terminate. |
| 1572 |
| 1573 size_t done_filename_len = filename_length + pid_str_length + 1; |
| 1574 char* done_filename = reinterpret_cast<char*>( |
| 1575 allocator.Alloc(done_filename_len)); |
| 1576 // Rename the file such that the pid is the suffix in order signal to other |
| 1577 // processes that the minidump is complete. The advantage of using the pid |
| 1578 // as the suffix is that it is trivial to associate the minidump with the |
| 1579 // crashed process. |
| 1580 my_strlcpy(done_filename, info.filename, done_filename_len); |
| 1581 my_strlcat(done_filename, pid_buf, done_filename_len); |
| 1582 // Rename the minidump file to signal that it is complete. |
| 1583 if (rename(info.filename, done_filename)) { |
| 1584 const char failed_msg[] = "Failed to rename:"; |
| 1585 WriteLog(failed_msg, sizeof(failed_msg) - 1); |
| 1586 WriteLog(info.filename, filename_length); |
| 1587 const char to_msg[] = "to"; |
| 1588 WriteLog(to_msg, sizeof(to_msg) - 1); |
| 1589 WriteLog(done_filename, done_filename_len - 1); |
| 1590 } |
| 1591 } |
| 1592 #endif |
| 1593 |
| 1594 if (!info.upload) |
| 1595 return; |
| 1596 |
| 1597 const pid_t child = sys_fork(); |
| 1598 if (!child) { |
| 1599 // Spawned helper process. |
| 1600 // |
| 1601 // This code is called both when a browser is crashing (in which case, |
| 1602 // nothing really matters any more) and when a renderer/plugin crashes, in |
| 1603 // which case we need to continue. |
| 1604 // |
| 1605 // Since we are a multithreaded app, if we were just to fork(), we might |
| 1606 // grab file descriptors which have just been created in another thread and |
| 1607 // hold them open for too long. |
| 1608 // |
| 1609 // Thus, we have to loop and try and close everything. |
| 1610 const int fd = sys_open("/proc/self/fd", O_DIRECTORY | O_RDONLY, 0); |
| 1611 if (fd < 0) { |
| 1612 for (unsigned i = 3; i < 8192; ++i) |
| 1613 IGNORE_RET(sys_close(i)); |
| 1614 } else { |
| 1615 google_breakpad::DirectoryReader reader(fd); |
| 1616 const char* name; |
| 1617 while (reader.GetNextEntry(&name)) { |
| 1618 int i; |
| 1619 if (my_strtoui(&i, name) && i > 2 && i != fd) |
| 1620 IGNORE_RET(sys_close(i)); |
| 1621 reader.PopEntry(); |
| 1622 } |
| 1623 |
| 1624 IGNORE_RET(sys_close(fd)); |
| 1625 } |
| 1626 |
| 1627 IGNORE_RET(sys_setsid()); |
| 1628 |
| 1629 // Leave one end of a pipe in the upload process and watch for it getting |
| 1630 // closed by the upload process exiting. |
| 1631 int fds[2]; |
| 1632 if (sys_pipe(fds) >= 0) { |
| 1633 const pid_t upload_child = sys_fork(); |
| 1634 if (!upload_child) { |
| 1635 // Upload process. |
| 1636 IGNORE_RET(sys_close(fds[0])); |
| 1637 IGNORE_RET(sys_dup2(fds[1], 3)); |
| 1638 ExecUploadProcessOrTerminate(info, temp_file, mime_boundary, exe_buf, |
| 1639 &allocator); |
| 1640 } |
| 1641 |
| 1642 // Helper process. |
| 1643 if (upload_child > 0) { |
| 1644 IGNORE_RET(sys_close(fds[1])); |
| 1645 |
| 1646 const size_t kCrashIdLength = 16; |
| 1647 char id_buf[kCrashIdLength + 1]; |
| 1648 size_t bytes_read = |
| 1649 WaitForCrashReportUploadProcess(fds[0], kCrashIdLength, id_buf); |
| 1650 HandleCrashReportId(id_buf, bytes_read, kCrashIdLength); |
| 1651 |
| 1652 if (sys_waitpid(upload_child, nullptr, WNOHANG) == 0) { |
| 1653 // Upload process is still around, kill it. |
| 1654 sys_kill(upload_child, SIGKILL); |
| 1655 } |
| 1656 } |
| 1657 } |
| 1658 |
| 1659 // Helper process. |
| 1660 IGNORE_RET(sys_unlink(info.filename)); |
| 1661 #if defined(ADDRESS_SANITIZER) |
| 1662 IGNORE_RET(sys_unlink(info.log_filename)); |
| 1663 #endif |
| 1664 IGNORE_RET(sys_unlink(temp_file)); |
| 1665 sys__exit(0); |
| 1666 } |
| 1667 |
| 1668 // Main browser process. |
| 1669 if (child <= 0) |
| 1670 return; |
| 1671 (void) HANDLE_EINTR(sys_waitpid(child, nullptr, 0)); |
| 1672 } |
| 1673 |
| 1674 void InitCrashReporter(const std::string& process_type) { |
| 1675 #if defined(OS_ANDROID) |
| 1676 // This will guarantee that the BuildInfo has been initialized and subsequent |
| 1677 // calls will not require memory allocation. |
| 1678 base::android::BuildInfo::GetInstance(); |
| 1679 |
| 1680 // Handler registration is LIFO. Install the microdump handler first, such |
| 1681 // that if conventional minidump crash reporting is enabled below, it takes |
| 1682 // precedence (i.e. its handler is run first) over the microdump handler. |
| 1683 InitMicrodumpCrashHandlerIfNecessary(process_type); |
| 1684 #endif |
| 1685 // Determine the process type and take appropriate action. |
| 1686 const base::CommandLine& parsed_command_line = |
| 1687 *base::CommandLine::ForCurrentProcess(); |
| 1688 if (parsed_command_line.HasSwitch(switches::kDisableBreakpad)) |
| 1689 return; |
| 1690 |
| 1691 if (process_type.empty()) { |
| 1692 bool enable_breakpad = GetCrashReporterClient()->GetCollectStatsConsent() || |
| 1693 GetCrashReporterClient()->IsRunningUnattended(); |
| 1694 enable_breakpad &= |
| 1695 !parsed_command_line.HasSwitch(switches::kDisableBreakpad); |
| 1696 if (!enable_breakpad) { |
| 1697 enable_breakpad = parsed_command_line.HasSwitch( |
| 1698 switches::kEnableCrashReporterForTesting); |
| 1699 } |
| 1700 if (!enable_breakpad) { |
| 1701 VLOG(1) << "Breakpad disabled"; |
| 1702 return; |
| 1703 } |
| 1704 |
| 1705 InitCrashKeys(); |
| 1706 EnableCrashDumping(GetCrashReporterClient()->IsRunningUnattended()); |
| 1707 } else if (GetCrashReporterClient()->EnableBreakpadForProcess(process_type)) { |
| 1708 #if defined(OS_ANDROID) |
| 1709 NOTREACHED() << "Breakpad initialized with InitCrashReporter() instead of " |
| 1710 "InitNonBrowserCrashReporter in " << process_type << " process."; |
| 1711 return; |
| 1712 #else |
| 1713 // We might be chrooted in a zygote or renderer process so we cannot call |
| 1714 // GetCollectStatsConsent because that needs access the the user's home |
| 1715 // dir. Instead, we set a command line flag for these processes. |
| 1716 // Even though plugins are not chrooted, we share the same code path for |
| 1717 // simplicity. |
| 1718 if (!parsed_command_line.HasSwitch(switches::kEnableCrashReporter)) |
| 1719 return; |
| 1720 InitCrashKeys(); |
| 1721 SetClientIdFromCommandLine(parsed_command_line); |
| 1722 EnableNonBrowserCrashDumping(); |
| 1723 VLOG(1) << "Non Browser crash dumping enabled for: " << process_type; |
| 1724 #endif // #if defined(OS_ANDROID) |
| 1725 } |
| 1726 |
| 1727 PostEnableBreakpadInitialization(); |
| 1728 } |
| 1729 |
| 1730 #if defined(OS_ANDROID) |
| 1731 void InitNonBrowserCrashReporterForAndroid(const std::string& process_type) { |
| 1732 const base::CommandLine* command_line = |
| 1733 base::CommandLine::ForCurrentProcess(); |
| 1734 |
| 1735 // Handler registration is LIFO. Install the microdump handler first, such |
| 1736 // that if conventional minidump crash reporting is enabled below, it takes |
| 1737 // precedence (i.e. its handler is run first) over the microdump handler. |
| 1738 InitMicrodumpCrashHandlerIfNecessary(process_type); |
| 1739 |
| 1740 if (command_line->HasSwitch(switches::kEnableCrashReporter)) { |
| 1741 // On Android we need to provide a FD to the file where the minidump is |
| 1742 // generated as the renderer and browser run with different UIDs |
| 1743 // (preventing the browser from inspecting the renderer process). |
| 1744 int minidump_fd = base::GlobalDescriptors::GetInstance()->MaybeGet( |
| 1745 GetCrashReporterClient()->GetAndroidMinidumpDescriptor()); |
| 1746 if (minidump_fd < 0) { |
| 1747 NOTREACHED() << "Could not find minidump FD, crash reporting disabled."; |
| 1748 } else { |
| 1749 InitCrashKeys(); |
| 1750 EnableNonBrowserCrashDumping(process_type, minidump_fd); |
| 1751 } |
| 1752 } |
| 1753 } |
| 1754 #endif // OS_ANDROID |
| 1755 |
| 1756 bool IsCrashReporterEnabled() { |
| 1757 return g_is_crash_reporter_enabled; |
| 1758 } |
| 1759 |
| 1760 } // namespace breakpad |
OLD | NEW |