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.
5 // For linux_syscall_support.h. This makes it safe to call embedded system
6 // calls when in seccomp mode.
8 #include "components/crash/app/breakpad_linux.h"
14 #include <sys/socket.h>
16 #include <sys/types.h>
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 "components/crash/app/breakpad_linux_impl.h"
42 #include "components/crash/app/crash_reporter_client.h"
43 #include "content/public/common/content_descriptors.h"
45 #if defined(OS_ANDROID)
46 #include <android/log.h>
49 #include "base/android/build_info.h"
50 #include "base/android/path_utils.h"
52 #include "third_party/lss/linux_syscall_support.h"
54 #if defined(ADDRESS_SANITIZER)
55 #include <ucontext.h> // for getcontext().
58 #if defined(OS_ANDROID)
59 #define STAT_STRUCT struct stat
60 #define FSTAT_FUNC fstat
62 #define STAT_STRUCT struct kernel_stat
63 #define FSTAT_FUNC sys_fstat
66 // Some versions of gcc are prone to warn about unused return values. In cases
67 // where we either a) know the call cannot fail, or b) there is nothing we
68 // can do when a call fails, we mark the return code as ignored. This avoids
69 // spurious compiler warnings.
70 #define IGNORE_RET(x) do { if (x); } while (0)
72 using crash_reporter::GetCrashReporterClient;
73 using google_breakpad::ExceptionHandler;
74 using google_breakpad::MinidumpDescriptor;
80 #if !defined(OS_CHROMEOS)
81 const char kUploadURL[] = "https://clients2.google.com/cr/report";
84 bool g_is_crash_reporter_enabled = false;
85 uint64_t g_process_start_time = 0;
87 char* g_crash_log_path = NULL;
88 ExceptionHandler* g_breakpad = NULL;
90 #if defined(ADDRESS_SANITIZER)
91 const char* g_asan_report_str = NULL;
93 #if defined(OS_ANDROID)
94 char* g_process_type = NULL;
97 CrashKeyStorage* g_crash_keys = NULL;
99 // Writes the value |v| as 16 hex characters to the memory pointed at by
101 void write_uint64_hex(char* output, uint64_t v) {
102 static const char hextable[] = "0123456789abcdef";
104 for (int i = 15; i >= 0; --i) {
105 output[i] = hextable[v & 15];
110 // The following helper functions are for calculating uptime.
112 // Converts a struct timeval to milliseconds.
113 uint64_t timeval_to_ms(struct timeval *tv) {
114 uint64_t ret = tv->tv_sec; // Avoid overflow by explicitly using a uint64_t.
116 ret += tv->tv_usec / 1000;
120 // Converts a struct timeval to milliseconds.
121 uint64_t kernel_timeval_to_ms(struct kernel_timeval *tv) {
122 uint64_t ret = tv->tv_sec; // Avoid overflow by explicitly using a uint64_t.
124 ret += tv->tv_usec / 1000;
128 // String buffer size to use to convert a uint64_t to string.
129 const size_t kUint64StringSize = 21;
131 void SetProcessStartTime() {
132 // Set the base process start time value.
134 if (!gettimeofday(&tv, NULL))
135 g_process_start_time = timeval_to_ms(&tv);
137 g_process_start_time = 0;
140 // uint64_t version of my_int_len() from
141 // breakpad/src/common/linux/linux_libc_support.h. Return the length of the
142 // given, non-negative integer when expressed in base 10.
143 unsigned my_uint64_len(uint64_t i) {
156 // uint64_t version of my_uitos() from
157 // breakpad/src/common/linux/linux_libc_support.h. Convert a non-negative
158 // integer to a string (not null-terminated).
159 void my_uint64tos(char* output, uint64_t i, unsigned i_len) {
160 for (unsigned index = i_len; index; --index, i /= 10)
161 output[index - 1] = '0' + (i % 10);
164 #if defined(OS_ANDROID)
165 char* my_strncpy(char* dst, const char* src, size_t len) {
170 while (i != 0 && *src != '\0') {
181 char* my_strncat(char *dest, const char* src, size_t len) {
186 if (!(*dest++ = *src++))
193 #if !defined(OS_CHROMEOS)
194 bool my_isxdigit(char c) {
195 return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'f');
199 size_t LengthWithoutTrailingSpaces(const char* str, size_t len) {
200 while (len > 0 && str[len - 1] == ' ') {
206 void SetClientIdFromCommandLine(const CommandLine& command_line) {
207 // Get the guid from the command line switch.
208 std::string switch_value =
209 command_line.GetSwitchValueASCII(switches::kEnableCrashReporter);
210 GetCrashReporterClient()->SetCrashReporterClientIdFromGUID(switch_value);
214 #if defined(OS_CHROMEOS)
215 const char g_sep[] = ":";
217 const char g_rn[] = "\r\n";
218 const char g_form_data_msg[] = "Content-Disposition: form-data; name=\"";
219 const char g_quote_msg[] = "\"";
220 const char g_dashdash_msg[] = "--";
221 const char g_dump_msg[] = "upload_file_minidump\"; filename=\"dump\"";
222 #if defined(ADDRESS_SANITIZER)
223 const char g_log_msg[] = "upload_file_log\"; filename=\"log\"";
225 const char g_content_type_msg[] = "Content-Type: application/octet-stream";
227 // MimeWriter manages an iovec for writing MIMEs to a file.
230 static const int kIovCapacity = 30;
231 static const size_t kMaxCrashChunkSize = 64;
233 MimeWriter(int fd, const char* const mime_boundary);
237 virtual void AddBoundary();
239 // Append end of file boundary.
240 virtual void AddEnd();
242 // Append key/value pair with specified sizes.
243 virtual void AddPairData(const char* msg_type,
244 size_t msg_type_size,
245 const char* msg_data,
246 size_t msg_data_size);
248 // Append key/value pair.
249 void AddPairString(const char* msg_type,
250 const char* msg_data) {
251 AddPairData(msg_type, my_strlen(msg_type), msg_data, my_strlen(msg_data));
254 // Append key/value pair, splitting value into chunks no larger than
255 // |chunk_size|. |chunk_size| cannot be greater than |kMaxCrashChunkSize|.
256 // The msg_type string will have a counter suffix to distinguish each chunk.
257 virtual void AddPairDataInChunks(const char* msg_type,
258 size_t msg_type_size,
259 const char* msg_data,
260 size_t msg_data_size,
262 bool strip_trailing_spaces);
264 // Add binary file contents to be uploaded with the specified filename.
265 virtual void AddFileContents(const char* filename_msg,
269 // Flush any pending iovecs to the output file.
271 IGNORE_RET(sys_writev(fd_, iov_, iov_index_));
276 void AddItem(const void* base, size_t size);
277 // Minor performance trade-off for easier-to-maintain code.
278 void AddString(const char* str) {
279 AddItem(str, my_strlen(str));
281 void AddItemWithoutTrailingSpaces(const void* base, size_t size);
283 struct kernel_iovec iov_[kIovCapacity];
286 // Output file descriptor.
289 const char* const mime_boundary_;
292 DISALLOW_COPY_AND_ASSIGN(MimeWriter);
295 MimeWriter::MimeWriter(int fd, const char* const mime_boundary)
298 mime_boundary_(mime_boundary) {
301 MimeWriter::~MimeWriter() {
304 void MimeWriter::AddBoundary() {
305 AddString(mime_boundary_);
309 void MimeWriter::AddEnd() {
310 AddString(mime_boundary_);
311 AddString(g_dashdash_msg);
315 void MimeWriter::AddPairData(const char* msg_type,
316 size_t msg_type_size,
317 const char* msg_data,
318 size_t msg_data_size) {
319 AddString(g_form_data_msg);
320 AddItem(msg_type, msg_type_size);
321 AddString(g_quote_msg);
324 AddItem(msg_data, msg_data_size);
328 void MimeWriter::AddPairDataInChunks(const char* msg_type,
329 size_t msg_type_size,
330 const char* msg_data,
331 size_t msg_data_size,
333 bool strip_trailing_spaces) {
334 if (chunk_size > kMaxCrashChunkSize)
338 size_t done = 0, msg_length = msg_data_size;
341 char num[kUint64StringSize];
342 const unsigned num_len = my_uint_len(++i);
343 my_uitos(num, i, num_len);
345 size_t chunk_len = std::min(chunk_size, msg_length);
347 AddString(g_form_data_msg);
348 AddItem(msg_type, msg_type_size);
349 AddItem(num, num_len);
350 AddString(g_quote_msg);
353 if (strip_trailing_spaces) {
354 AddItemWithoutTrailingSpaces(msg_data + done, chunk_len);
356 AddItem(msg_data + done, chunk_len);
363 msg_length -= chunk_len;
367 void MimeWriter::AddFileContents(const char* filename_msg, uint8_t* file_data,
369 AddString(g_form_data_msg);
370 AddString(filename_msg);
372 AddString(g_content_type_msg);
375 AddItem(file_data, file_size);
379 void MimeWriter::AddItem(const void* base, size_t size) {
380 // Check if the iovec is full and needs to be flushed to output file.
381 if (iov_index_ == kIovCapacity) {
384 iov_[iov_index_].iov_base = const_cast<void*>(base);
385 iov_[iov_index_].iov_len = size;
389 void MimeWriter::AddItemWithoutTrailingSpaces(const void* base, size_t size) {
390 AddItem(base, LengthWithoutTrailingSpaces(static_cast<const char*>(base),
394 #if defined(OS_CHROMEOS)
395 // This subclass is used on Chromium OS to report crashes in a format easy for
396 // the central crash reporting facility to understand.
397 // Format is <name>:<data length in decimal>:<data>
398 class CrashReporterWriter : public MimeWriter {
400 explicit CrashReporterWriter(int fd);
402 virtual void AddBoundary() OVERRIDE;
404 virtual void AddEnd() OVERRIDE;
406 virtual void AddPairData(const char* msg_type,
407 size_t msg_type_size,
408 const char* msg_data,
409 size_t msg_data_size) OVERRIDE;
411 virtual void AddPairDataInChunks(const char* msg_type,
412 size_t msg_type_size,
413 const char* msg_data,
414 size_t msg_data_size,
416 bool strip_trailing_spaces) OVERRIDE;
418 virtual void AddFileContents(const char* filename_msg,
420 size_t file_size) OVERRIDE;
423 DISALLOW_COPY_AND_ASSIGN(CrashReporterWriter);
427 CrashReporterWriter::CrashReporterWriter(int fd) : MimeWriter(fd, "") {}
430 void CrashReporterWriter::AddBoundary() {}
431 void CrashReporterWriter::AddEnd() {}
433 void CrashReporterWriter::AddPairData(const char* msg_type,
434 size_t msg_type_size,
435 const char* msg_data,
436 size_t msg_data_size) {
437 char data[kUint64StringSize];
438 const unsigned data_len = my_uint_len(msg_data_size);
439 my_uitos(data, msg_data_size, data_len);
441 AddItem(msg_type, msg_type_size);
443 AddItem(data, data_len);
445 AddItem(msg_data, msg_data_size);
449 void CrashReporterWriter::AddPairDataInChunks(const char* msg_type,
450 size_t msg_type_size,
451 const char* msg_data,
452 size_t msg_data_size,
454 bool strip_trailing_spaces) {
455 if (chunk_size > kMaxCrashChunkSize)
460 size_t msg_length = msg_data_size;
463 char num[kUint64StringSize];
464 const unsigned num_len = my_uint_len(++i);
465 my_uitos(num, i, num_len);
467 size_t chunk_len = std::min(chunk_size, msg_length);
469 size_t write_len = chunk_len;
470 if (strip_trailing_spaces) {
471 // Take care of this here because we need to know the exact length of
472 // what is going to be written.
473 write_len = LengthWithoutTrailingSpaces(msg_data + done, write_len);
476 char data[kUint64StringSize];
477 const unsigned data_len = my_uint_len(write_len);
478 my_uitos(data, write_len, data_len);
480 AddItem(msg_type, msg_type_size);
481 AddItem(num, num_len);
483 AddItem(data, data_len);
485 AddItem(msg_data + done, write_len);
489 msg_length -= chunk_len;
493 void CrashReporterWriter::AddFileContents(const char* filename_msg,
496 char data[kUint64StringSize];
497 const unsigned data_len = my_uint_len(file_size);
498 my_uitos(data, file_size, data_len);
500 AddString(filename_msg);
502 AddItem(data, data_len);
504 AddItem(file_data, file_size);
507 #endif // defined(OS_CHROMEOS)
511 g_breakpad->WriteMinidump();
514 #if defined(OS_ANDROID)
515 const char kGoogleBreakpad[] = "google-breakpad";
518 size_t WriteLog(const char* buf, size_t nbytes) {
519 #if defined(OS_ANDROID)
520 return __android_log_write(ANDROID_LOG_WARN, kGoogleBreakpad, buf);
522 return sys_write(2, buf, nbytes);
526 size_t WriteNewline() {
527 return WriteLog("\n", 1);
530 #if defined(OS_ANDROID)
531 // Android's native crash handler outputs a diagnostic tombstone to the device
532 // log. By returning false from the HandlerCallbacks, breakpad will reinstall
533 // the previous (i.e. native) signal handlers before returning from its own
534 // handler. A Chrome build fingerprint is written to the log, so that the
535 // specific build of Chrome and the location of the archived Chrome symbols can
536 // be determined directly from it.
537 bool FinalizeCrashDoneAndroid() {
538 base::android::BuildInfo* android_build_info =
539 base::android::BuildInfo::GetInstance();
541 __android_log_write(ANDROID_LOG_WARN, kGoogleBreakpad,
542 "### ### ### ### ### ### ### ### ### ### ### ### ###");
543 __android_log_write(ANDROID_LOG_WARN, kGoogleBreakpad,
544 "Chrome build fingerprint:");
545 __android_log_write(ANDROID_LOG_WARN, kGoogleBreakpad,
546 android_build_info->package_version_name());
547 __android_log_write(ANDROID_LOG_WARN, kGoogleBreakpad,
548 android_build_info->package_version_code());
549 __android_log_write(ANDROID_LOG_WARN, kGoogleBreakpad,
551 __android_log_write(ANDROID_LOG_WARN, kGoogleBreakpad,
552 "### ### ### ### ### ### ### ### ### ### ### ### ###");
557 bool CrashDone(const MinidumpDescriptor& minidump,
559 const bool succeeded) {
560 // WARNING: this code runs in a compromised context. It may not call into
561 // libc nor allocate memory normally.
563 const char msg[] = "Failed to generate minidump.";
564 WriteLog(msg, sizeof(msg) - 1);
568 DCHECK(!minidump.IsFD());
570 BreakpadInfo info = {0};
571 info.filename = minidump.path();
572 info.fd = minidump.fd();
573 #if defined(ADDRESS_SANITIZER)
574 google_breakpad::PageAllocator allocator;
575 const size_t log_path_len = my_strlen(minidump.path());
576 char* log_path = reinterpret_cast<char*>(allocator.Alloc(log_path_len + 1));
577 my_memcpy(log_path, minidump.path(), log_path_len);
578 my_memcpy(log_path + log_path_len - 4, ".log", 4);
579 log_path[log_path_len] = '\0';
580 info.log_filename = log_path;
582 info.process_type = "browser";
583 info.process_type_length = 7;
584 info.distro = base::g_linux_distro;
585 info.distro_length = my_strlen(base::g_linux_distro);
586 info.upload = upload;
587 info.process_start_time = g_process_start_time;
588 info.oom_size = base::g_oom_size;
590 info.crash_keys = g_crash_keys;
591 HandleCrashDump(info);
592 #if defined(OS_ANDROID)
593 return FinalizeCrashDoneAndroid();
599 // Wrapper function, do not add more code here.
600 bool CrashDoneNoUpload(const MinidumpDescriptor& minidump,
603 return CrashDone(minidump, false, succeeded);
606 #if !defined(OS_ANDROID)
607 // Wrapper function, do not add more code here.
608 bool CrashDoneUpload(const MinidumpDescriptor& minidump,
611 return CrashDone(minidump, true, succeeded);
615 #if defined(ADDRESS_SANITIZER)
617 void __asan_set_error_report_callback(void (*cb)(const char*));
620 void AsanLinuxBreakpadCallback(const char* report) {
621 g_asan_report_str = report;
622 // Send minidump here.
623 g_breakpad->SimulateSignalDelivery(SIGKILL);
627 void EnableCrashDumping(bool unattended) {
628 g_is_crash_reporter_enabled = true;
630 base::FilePath tmp_path("/tmp");
631 PathService::Get(base::DIR_TEMP, &tmp_path);
633 base::FilePath dumps_path(tmp_path);
634 if (GetCrashReporterClient()->GetCrashDumpLocation(&dumps_path)) {
635 base::FilePath logfile =
636 dumps_path.Append(GetCrashReporterClient()->GetReporterLogFilename());
637 std::string logfile_str = logfile.value();
638 const size_t crash_log_path_len = logfile_str.size() + 1;
639 g_crash_log_path = new char[crash_log_path_len];
640 strncpy(g_crash_log_path, logfile_str.c_str(), crash_log_path_len);
643 MinidumpDescriptor minidump_descriptor(dumps_path.value());
644 minidump_descriptor.set_size_limit(kMaxMinidumpFileSize);
645 #if defined(OS_ANDROID)
646 unattended = true; // Android never uploads directly.
649 g_breakpad = new ExceptionHandler(
654 true, // Install handlers.
655 -1); // Server file descriptor. -1 for in-process.
659 #if !defined(OS_ANDROID)
661 g_breakpad = new ExceptionHandler(
666 true, // Install handlers.
667 -1); // Server file descriptor. -1 for in-process.
671 #if defined(OS_ANDROID)
672 bool CrashDoneInProcessNoUpload(
673 const google_breakpad::MinidumpDescriptor& descriptor,
675 const bool succeeded) {
676 // WARNING: this code runs in a compromised context. It may not call into
677 // libc nor allocate memory normally.
679 static const char msg[] = "Crash dump generation failed.\n";
680 WriteLog(msg, sizeof(msg) - 1);
684 // Start constructing the message to send to the browser.
685 BreakpadInfo info = {0};
686 info.filename = NULL;
687 info.fd = descriptor.fd();
688 info.process_type = g_process_type;
689 info.process_type_length = my_strlen(g_process_type);
691 info.distro_length = 0;
693 info.process_start_time = g_process_start_time;
695 info.crash_keys = g_crash_keys;
696 HandleCrashDump(info);
697 bool finalize_result = FinalizeCrashDoneAndroid();
698 base::android::BuildInfo* android_build_info =
699 base::android::BuildInfo::GetInstance();
700 if (android_build_info->sdk_int() >= 18 &&
701 my_strcmp(android_build_info->build_type(), "eng") != 0 &&
702 my_strcmp(android_build_info->build_type(), "userdebug") != 0) {
703 // On JB MR2 and later, the system crash handler displays a dialog. For
704 // renderer crashes, this is a bad user experience and so this is disabled
705 // for user builds of Android.
706 // TODO(cjhopman): There should be some way to recover the crash stack from
707 // non-uploading user clients. See http://crbug.com/273706.
708 __android_log_write(ANDROID_LOG_WARN,
710 "Tombstones are disabled on JB MR2+ user builds.");
711 __android_log_write(ANDROID_LOG_WARN,
713 "### ### ### ### ### ### ### ### ### ### ### ### ###");
716 return finalize_result;
720 void EnableNonBrowserCrashDumping(const std::string& process_type,
722 // This will guarantee that the BuildInfo has been initialized and subsequent
723 // calls will not require memory allocation.
724 base::android::BuildInfo::GetInstance();
725 SetClientIdFromCommandLine(*CommandLine::ForCurrentProcess());
727 // On Android, the current sandboxing uses process isolation, in which the
728 // child process runs with a different UID. That breaks the normal crash
729 // reporting where the browser process generates the minidump by inspecting
730 // the child process. This is because the browser process now does not have
731 // the permission to access the states of the child process (as it has a
733 // TODO(jcivelli): http://b/issue?id=6776356 we should use a watchdog
734 // process forked from the renderer process that generates the minidump.
735 if (minidump_fd == -1) {
736 LOG(ERROR) << "Minidump file descriptor not found, crash reporting will "
740 SetProcessStartTime();
743 g_is_crash_reporter_enabled = true;
744 // Save the process type (it is leaked).
745 const size_t process_type_len = process_type.size() + 1;
746 g_process_type = new char[process_type_len];
747 strncpy(g_process_type, process_type.c_str(), process_type_len);
748 new google_breakpad::ExceptionHandler(MinidumpDescriptor(minidump_fd),
749 NULL, CrashDoneInProcessNoUpload, NULL, true, -1);
752 // Non-Browser = Extension, Gpu, Plugins, Ppapi and Renderer
753 class NonBrowserCrashHandler : public google_breakpad::CrashGenerationClient {
755 NonBrowserCrashHandler()
756 : server_fd_(base::GlobalDescriptors::GetInstance()->Get(
760 virtual ~NonBrowserCrashHandler() {}
762 virtual bool RequestDump(const void* crash_context,
763 size_t crash_context_size) OVERRIDE {
764 int fds[2] = { -1, -1 };
765 if (sys_socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
766 static const char msg[] = "Failed to create socket for crash dumping.\n";
767 WriteLog(msg, sizeof(msg) - 1);
771 // Start constructing the message to send to the browser.
772 char b; // Dummy variable for sys_read below.
773 const char* b_addr = &b; // Get the address of |b| so we can create the
774 // expected /proc/[pid]/syscall content in the
775 // browser to convert namespace tids.
777 // The length of the control message:
778 static const unsigned kControlMsgSize = sizeof(int);
779 static const unsigned kControlMsgSpaceSize = CMSG_SPACE(kControlMsgSize);
780 static const unsigned kControlMsgLenSize = CMSG_LEN(kControlMsgSize);
782 struct kernel_msghdr msg;
783 my_memset(&msg, 0, sizeof(struct kernel_msghdr));
784 struct kernel_iovec iov[kCrashIovSize];
785 iov[0].iov_base = const_cast<void*>(crash_context);
786 iov[0].iov_len = crash_context_size;
787 iov[1].iov_base = &b_addr;
788 iov[1].iov_len = sizeof(b_addr);
789 iov[2].iov_base = &fds[0];
790 iov[2].iov_len = sizeof(fds[0]);
791 iov[3].iov_base = &g_process_start_time;
792 iov[3].iov_len = sizeof(g_process_start_time);
793 iov[4].iov_base = &base::g_oom_size;
794 iov[4].iov_len = sizeof(base::g_oom_size);
795 google_breakpad::SerializedNonAllocatingMap* serialized_map;
796 iov[5].iov_len = g_crash_keys->Serialize(
797 const_cast<const google_breakpad::SerializedNonAllocatingMap**>(
799 iov[5].iov_base = serialized_map;
800 #if !defined(ADDRESS_SANITIZER)
801 COMPILE_ASSERT(5 == kCrashIovSize - 1, Incorrect_Number_Of_Iovec_Members);
803 iov[6].iov_base = const_cast<char*>(g_asan_report_str);
804 iov[6].iov_len = kMaxAsanReportSize + 1;
805 COMPILE_ASSERT(6 == kCrashIovSize - 1, Incorrect_Number_Of_Iovec_Members);
809 msg.msg_iovlen = kCrashIovSize;
810 char cmsg[kControlMsgSpaceSize];
811 my_memset(cmsg, 0, kControlMsgSpaceSize);
812 msg.msg_control = cmsg;
813 msg.msg_controllen = sizeof(cmsg);
815 struct cmsghdr *hdr = CMSG_FIRSTHDR(&msg);
816 hdr->cmsg_level = SOL_SOCKET;
817 hdr->cmsg_type = SCM_RIGHTS;
818 hdr->cmsg_len = kControlMsgLenSize;
819 ((int*)CMSG_DATA(hdr))[0] = fds[1];
821 if (HANDLE_EINTR(sys_sendmsg(server_fd_, &msg, 0)) < 0) {
822 static const char errmsg[] = "Failed to tell parent about crash.\n";
823 WriteLog(errmsg, sizeof(errmsg) - 1);
824 IGNORE_RET(sys_close(fds[0]));
825 IGNORE_RET(sys_close(fds[1]));
828 IGNORE_RET(sys_close(fds[1]));
830 if (HANDLE_EINTR(sys_read(fds[0], &b, 1)) != 1) {
831 static const char errmsg[] = "Parent failed to complete crash dump.\n";
832 WriteLog(errmsg, sizeof(errmsg) - 1);
834 IGNORE_RET(sys_close(fds[0]));
840 // The pipe FD to the browser process, which will handle the crash dumping.
841 const int server_fd_;
843 DISALLOW_COPY_AND_ASSIGN(NonBrowserCrashHandler);
846 void EnableNonBrowserCrashDumping() {
847 g_is_crash_reporter_enabled = true;
848 // We deliberately leak this object.
851 g_breakpad = new ExceptionHandler(
852 MinidumpDescriptor("/tmp"), // Unused but needed or Breakpad will assert.
858 g_breakpad->set_crash_generation_client(new NonBrowserCrashHandler());
860 #endif // defined(OS_ANDROID)
862 void SetCrashKeyValue(const base::StringPiece& key,
863 const base::StringPiece& value) {
864 g_crash_keys->SetKeyValue(key.data(), value.data());
867 void ClearCrashKey(const base::StringPiece& key) {
868 g_crash_keys->RemoveKey(key.data());
871 // GetCrashReporterClient() cannot call any Set methods until after
873 void InitCrashKeys() {
874 g_crash_keys = new CrashKeyStorage;
875 GetCrashReporterClient()->RegisterCrashKeys();
876 base::debug::SetCrashKeyReportingFunctions(&SetCrashKeyValue, &ClearCrashKey);
879 // Miscellaneous initialization functions to call after Breakpad has been
881 void PostEnableBreakpadInitialization() {
882 SetProcessStartTime();
885 base::debug::SetDumpWithoutCrashingFunction(&DumpProcess);
886 #if defined(ADDRESS_SANITIZER)
887 // Register the callback for AddressSanitizer error reporting.
888 __asan_set_error_report_callback(AsanLinuxBreakpadCallback);
894 void LoadDataFromFD(google_breakpad::PageAllocator& allocator,
895 int fd, bool close_fd, uint8_t** file_data, size_t* size) {
897 if (FSTAT_FUNC(fd, &st) != 0) {
898 static const char msg[] = "Cannot upload crash dump: stat failed\n";
899 WriteLog(msg, sizeof(msg) - 1);
901 IGNORE_RET(sys_close(fd));
905 *file_data = reinterpret_cast<uint8_t*>(allocator.Alloc(st.st_size));
907 static const char msg[] = "Cannot upload crash dump: cannot alloc\n";
908 WriteLog(msg, sizeof(msg) - 1);
910 IGNORE_RET(sys_close(fd));
913 my_memset(*file_data, 0xf, st.st_size);
916 int byte_read = sys_read(fd, *file_data, *size);
917 if (byte_read == -1) {
918 static const char msg[] = "Cannot upload crash dump: read failed\n";
919 WriteLog(msg, sizeof(msg) - 1);
921 IGNORE_RET(sys_close(fd));
926 IGNORE_RET(sys_close(fd));
929 void LoadDataFromFile(google_breakpad::PageAllocator& allocator,
930 const char* filename,
931 int* fd, uint8_t** file_data, size_t* size) {
932 // WARNING: this code runs in a compromised context. It may not call into
933 // libc nor allocate memory normally.
934 *fd = sys_open(filename, O_RDONLY, 0);
938 static const char msg[] = "Cannot upload crash dump: failed to open\n";
939 WriteLog(msg, sizeof(msg) - 1);
943 LoadDataFromFD(allocator, *fd, true, file_data, size);
946 // Spawn the appropriate upload process for the current OS:
947 // - generic Linux invokes wget.
948 // - ChromeOS invokes crash_reporter.
949 // |dumpfile| is the path to the dump data file.
950 // |mime_boundary| is only used on Linux.
951 // |exe_buf| is only used on CrOS and is the crashing process' name.
952 void ExecUploadProcessOrTerminate(const BreakpadInfo& info,
953 const char* dumpfile,
954 const char* mime_boundary,
956 google_breakpad::PageAllocator* allocator) {
957 #if defined(OS_CHROMEOS)
958 // CrOS uses crash_reporter instead of wget to report crashes,
959 // it needs to know where the crash dump lives and the pid and uid of the
961 static const char kCrashReporterBinary[] = "/sbin/crash_reporter";
963 char pid_buf[kUint64StringSize];
964 uint64_t pid_str_length = my_uint64_len(info.pid);
965 my_uint64tos(pid_buf, info.pid, pid_str_length);
966 pid_buf[pid_str_length] = '\0';
968 char uid_buf[kUint64StringSize];
969 uid_t uid = geteuid();
970 uint64_t uid_str_length = my_uint64_len(uid);
971 my_uint64tos(uid_buf, uid, uid_str_length);
972 uid_buf[uid_str_length] = '\0';
973 const char* args[] = {
974 kCrashReporterBinary,
985 static const char msg[] = "Cannot upload crash dump: cannot exec "
986 "/sbin/crash_reporter\n";
988 // The --header argument to wget looks like:
989 // --header=Content-Type: multipart/form-data; boundary=XYZ
990 // where the boundary has two fewer leading '-' chars
991 static const char header_msg[] =
992 "--header=Content-Type: multipart/form-data; boundary=";
993 char* const header = reinterpret_cast<char*>(allocator->Alloc(
994 sizeof(header_msg) - 1 + strlen(mime_boundary) - 2 + 1));
995 memcpy(header, header_msg, sizeof(header_msg) - 1);
996 memcpy(header + sizeof(header_msg) - 1, mime_boundary + 2,
997 strlen(mime_boundary) - 2);
998 // We grab the NUL byte from the end of |mime_boundary|.
1000 // The --post-file argument to wget looks like:
1001 // --post-file=/tmp/...
1002 static const char post_file_msg[] = "--post-file=";
1003 char* const post_file = reinterpret_cast<char*>(allocator->Alloc(
1004 sizeof(post_file_msg) - 1 + strlen(dumpfile) + 1));
1005 memcpy(post_file, post_file_msg, sizeof(post_file_msg) - 1);
1006 memcpy(post_file + sizeof(post_file_msg) - 1, dumpfile, strlen(dumpfile));
1008 static const char kWgetBinary[] = "/usr/bin/wget";
1009 const char* args[] = {
1014 "--timeout=10", // Set a timeout so we don't hang forever.
1015 "--tries=1", // Don't retry if the upload fails.
1016 "-O", // output reply to fd 3
1020 static const char msg[] = "Cannot upload crash dump: cannot exec "
1023 execve(args[0], const_cast<char**>(args), environ);
1024 WriteLog(msg, sizeof(msg) - 1);
1028 // Runs in the helper process to wait for the upload process running
1029 // ExecUploadProcessOrTerminate() to finish. Returns the number of bytes written
1030 // to |fd| and save the written contents to |buf|.
1031 // |buf| needs to be big enough to hold |bytes_to_read| + 1 characters.
1032 size_t WaitForCrashReportUploadProcess(int fd, size_t bytes_to_read,
1034 size_t bytes_read = 0;
1036 // Upload should finish in about 10 seconds. Add a few more 500 ms
1037 // internals to account for process startup time.
1038 for (size_t wait_count = 0; wait_count < 24; ++wait_count) {
1039 struct kernel_pollfd poll_fd;
1041 poll_fd.events = POLLIN | POLLPRI | POLLERR;
1042 int ret = sys_poll(&poll_fd, 1, 500);
1046 } else if (ret > 0) {
1047 // There is data to read.
1048 ssize_t len = HANDLE_EINTR(
1049 sys_read(fd, buf + bytes_read, bytes_to_read - bytes_read));
1053 if (bytes_read == bytes_to_read)
1056 // |ret| == 0 -> timed out, continue waiting.
1057 // or |bytes_read| < |bytes_to_read| still, keep reading.
1059 buf[bytes_to_read] = 0; // Always NUL terminate the buffer.
1063 // |buf| should be |expected_len| + 1 characters in size and NULL terminated.
1064 bool IsValidCrashReportId(const char* buf, size_t bytes_read,
1065 size_t expected_len) {
1066 if (bytes_read != expected_len)
1068 #if defined(OS_CHROMEOS)
1069 return my_strcmp(buf, "_sys_cr_finished") == 0;
1071 for (size_t i = 0; i < bytes_read; ++i) {
1072 if (!my_isxdigit(buf[i]))
1079 // |buf| should be |expected_len| + 1 characters in size and NULL terminated.
1080 void HandleCrashReportId(const char* buf, size_t bytes_read,
1081 size_t expected_len) {
1083 if (!IsValidCrashReportId(buf, bytes_read, expected_len)) {
1084 #if defined(OS_CHROMEOS)
1085 static const char msg[] =
1086 "System crash-reporter failed to process crash report.";
1088 static const char msg[] = "Failed to get crash dump id.";
1090 WriteLog(msg, sizeof(msg) - 1);
1093 static const char id_msg[] = "Report Id: ";
1094 WriteLog(id_msg, sizeof(id_msg) - 1);
1095 WriteLog(buf, bytes_read);
1100 #if defined(OS_CHROMEOS)
1101 static const char msg[] = "Crash dump received by crash_reporter\n";
1102 WriteLog(msg, sizeof(msg) - 1);
1104 // Write crash dump id to stderr.
1105 static const char msg[] = "Crash dump id: ";
1106 WriteLog(msg, sizeof(msg) - 1);
1107 WriteLog(buf, my_strlen(buf));
1110 // Write crash dump id to crash log as: seconds_since_epoch,crash_id
1111 struct kernel_timeval tv;
1112 if (g_crash_log_path && !sys_gettimeofday(&tv, NULL)) {
1113 uint64_t time = kernel_timeval_to_ms(&tv) / 1000;
1114 char time_str[kUint64StringSize];
1115 const unsigned time_len = my_uint64_len(time);
1116 my_uint64tos(time_str, time, time_len);
1118 const int kLogOpenFlags = O_CREAT | O_WRONLY | O_APPEND | O_CLOEXEC;
1119 int log_fd = sys_open(g_crash_log_path, kLogOpenFlags, 0600);
1121 sys_write(log_fd, time_str, time_len);
1122 sys_write(log_fd, ",", 1);
1123 sys_write(log_fd, buf, my_strlen(buf));
1124 sys_write(log_fd, "\n", 1);
1125 IGNORE_RET(sys_close(log_fd));
1131 #if defined(OS_CHROMEOS)
1132 const char* GetCrashingProcessName(const BreakpadInfo& info,
1133 google_breakpad::PageAllocator* allocator) {
1134 // Symlink to process binary is at /proc/###/exe.
1135 char linkpath[kUint64StringSize + sizeof("/proc/") + sizeof("/exe")] =
1137 uint64_t pid_value_len = my_uint64_len(info.pid);
1138 my_uint64tos(linkpath + sizeof("/proc/") - 1, info.pid, pid_value_len);
1139 linkpath[sizeof("/proc/") - 1 + pid_value_len] = '\0';
1140 my_strlcat(linkpath, "/exe", sizeof(linkpath));
1142 const int kMaxSize = 4096;
1143 char* link = reinterpret_cast<char*>(allocator->Alloc(kMaxSize));
1145 ssize_t size = readlink(linkpath, link, kMaxSize);
1146 if (size < kMaxSize && size > 0) {
1147 // readlink(2) doesn't add a terminating NUL, so do it now.
1150 const char* name = my_strrchr(link, '/');
1156 // Either way too long, or a read error.
1157 return "chrome-crash-unknown-process";
1161 void HandleCrashDump(const BreakpadInfo& info) {
1163 bool keep_fd = false;
1166 google_breakpad::PageAllocator allocator;
1167 const char* exe_buf = NULL;
1169 #if defined(OS_CHROMEOS)
1170 // Grab the crashing process' name now, when it should still be available.
1171 // If we try to do this later in our grandchild the crashing process has
1172 // already terminated.
1173 exe_buf = GetCrashingProcessName(info, &allocator);
1176 if (info.fd != -1) {
1177 // Dump is provided with an open FD.
1181 // The FD is pointing to the end of the file.
1182 // Rewind, we'll read the data next.
1183 if (lseek(dumpfd, 0, SEEK_SET) == -1) {
1184 static const char msg[] = "Cannot upload crash dump: failed to "
1185 "reposition minidump FD\n";
1186 WriteLog(msg, sizeof(msg) - 1);
1187 IGNORE_RET(sys_close(dumpfd));
1190 LoadDataFromFD(allocator, info.fd, false, &dump_data, &dump_size);
1192 // Dump is provided with a path.
1194 LoadDataFromFile(allocator, info.filename, &dumpfd, &dump_data, &dump_size);
1197 // TODO(jcivelli): make log work when using FDs.
1198 #if defined(ADDRESS_SANITIZER)
1202 // Load the AddressSanitizer log into log_data.
1203 LoadDataFromFile(allocator, info.log_filename, &logfd, &log_data, &log_size);
1206 // We need to build a MIME block for uploading to the server. Since we are
1207 // going to fork and run wget, it needs to be written to a temp file.
1208 const int ufd = sys_open("/dev/urandom", O_RDONLY, 0);
1210 static const char msg[] = "Cannot upload crash dump because /dev/urandom"
1212 WriteLog(msg, sizeof(msg) - 1);
1216 static const char temp_file_template[] =
1217 "/tmp/chromium-upload-XXXXXXXXXXXXXXXX";
1218 char temp_file[sizeof(temp_file_template)];
1219 int temp_file_fd = -1;
1221 temp_file_fd = dumpfd;
1222 // Rewind the destination, we are going to overwrite it.
1223 if (lseek(dumpfd, 0, SEEK_SET) == -1) {
1224 static const char msg[] = "Cannot upload crash dump: failed to "
1225 "reposition minidump FD (2)\n";
1226 WriteLog(msg, sizeof(msg) - 1);
1227 IGNORE_RET(sys_close(dumpfd));
1232 memcpy(temp_file, temp_file_template, sizeof(temp_file_template));
1234 for (unsigned i = 0; i < 10; ++i) {
1236 sys_read(ufd, &t, sizeof(t));
1237 write_uint64_hex(temp_file + sizeof(temp_file) - (16 + 1), t);
1239 temp_file_fd = sys_open(temp_file, O_WRONLY | O_CREAT | O_EXCL, 0600);
1240 if (temp_file_fd >= 0)
1244 if (temp_file_fd < 0) {
1245 static const char msg[] = "Failed to create temporary file in /tmp: "
1246 "cannot upload crash dump\n";
1247 WriteLog(msg, sizeof(msg) - 1);
1248 IGNORE_RET(sys_close(ufd));
1252 temp_file_fd = sys_open(info.filename, O_WRONLY, 0600);
1253 if (temp_file_fd < 0) {
1254 static const char msg[] = "Failed to save crash dump: failed to open\n";
1255 WriteLog(msg, sizeof(msg) - 1);
1256 IGNORE_RET(sys_close(ufd));
1262 // The MIME boundary is 28 hyphens, followed by a 64-bit nonce and a NUL.
1263 char mime_boundary[28 + 16 + 1];
1264 my_memset(mime_boundary, '-', 28);
1265 uint64_t boundary_rand;
1266 sys_read(ufd, &boundary_rand, sizeof(boundary_rand));
1267 write_uint64_hex(mime_boundary + 28, boundary_rand);
1268 mime_boundary[28 + 16] = 0;
1269 IGNORE_RET(sys_close(ufd));
1271 // The MIME block looks like this:
1273 // Content-Disposition: form-data; name="prod" \r\n \r\n
1274 // Chrome_Linux \r\n
1276 // Content-Disposition: form-data; name="ver" \r\n \r\n
1281 // Content-Disposition: form-data; name="ptime" \r\n \r\n
1286 // Content-Disposition: form-data; name="ptype" \r\n \r\n
1291 // Content-Disposition: form-data; name="lsb-release" \r\n \r\n
1296 // Content-Disposition: form-data; name="oom-size" \r\n \r\n
1300 // zero or more (up to CrashKeyStorage::num_entries = 64):
1301 // Content-Disposition: form-data; name=crash-key-name \r\n
1302 // crash-key-value \r\n
1305 // Content-Disposition: form-data; name="dump"; filename="dump" \r\n
1306 // Content-Type: application/octet-stream \r\n \r\n
1308 // \r\n BOUNDARY -- \r\n
1310 #if defined(OS_CHROMEOS)
1311 CrashReporterWriter writer(temp_file_fd);
1313 MimeWriter writer(temp_file_fd, mime_boundary);
1316 // TODO(thestig) Do not use this inside a compromised context.
1317 std::string product_name;
1318 std::string version;
1320 GetCrashReporterClient()->GetProductNameAndVersion(&product_name, &version);
1322 writer.AddBoundary();
1323 writer.AddPairString("prod", product_name.c_str());
1324 writer.AddBoundary();
1325 writer.AddPairString("ver", version.c_str());
1326 writer.AddBoundary();
1328 char pid_value_buf[kUint64StringSize];
1329 uint64_t pid_value_len = my_uint64_len(info.pid);
1330 my_uint64tos(pid_value_buf, info.pid, pid_value_len);
1331 static const char pid_key_name[] = "pid";
1332 writer.AddPairData(pid_key_name, sizeof(pid_key_name) - 1,
1333 pid_value_buf, pid_value_len);
1334 writer.AddBoundary();
1336 #if defined(OS_ANDROID)
1337 // Addtional MIME blocks are added for logging on Android devices.
1338 static const char android_build_id[] = "android_build_id";
1339 static const char android_build_fp[] = "android_build_fp";
1340 static const char device[] = "device";
1341 static const char model[] = "model";
1342 static const char brand[] = "brand";
1343 static const char exception_info[] = "exception_info";
1345 base::android::BuildInfo* android_build_info =
1346 base::android::BuildInfo::GetInstance();
1347 writer.AddPairString(
1348 android_build_id, android_build_info->android_build_id());
1349 writer.AddBoundary();
1350 writer.AddPairString(
1351 android_build_fp, android_build_info->android_build_fp());
1352 writer.AddBoundary();
1353 writer.AddPairString(device, android_build_info->device());
1354 writer.AddBoundary();
1355 writer.AddPairString(model, android_build_info->model());
1356 writer.AddBoundary();
1357 writer.AddPairString(brand, android_build_info->brand());
1358 writer.AddBoundary();
1359 if (android_build_info->java_exception_info() != NULL) {
1360 writer.AddPairString(exception_info,
1361 android_build_info->java_exception_info());
1362 writer.AddBoundary();
1368 if (info.process_start_time > 0) {
1369 struct kernel_timeval tv;
1370 if (!sys_gettimeofday(&tv, NULL)) {
1371 uint64_t time = kernel_timeval_to_ms(&tv);
1372 if (time > info.process_start_time) {
1373 time -= info.process_start_time;
1374 char time_str[kUint64StringSize];
1375 const unsigned time_len = my_uint64_len(time);
1376 my_uint64tos(time_str, time, time_len);
1378 static const char process_time_msg[] = "ptime";
1379 writer.AddPairData(process_time_msg, sizeof(process_time_msg) - 1,
1380 time_str, time_len);
1381 writer.AddBoundary();
1387 if (info.process_type_length) {
1388 writer.AddPairString("ptype", info.process_type);
1389 writer.AddBoundary();
1393 if (info.distro_length) {
1394 static const char distro_msg[] = "lsb-release";
1395 writer.AddPairString(distro_msg, info.distro);
1396 writer.AddBoundary();
1400 if (info.oom_size) {
1401 char oom_size_str[kUint64StringSize];
1402 const unsigned oom_size_len = my_uint64_len(info.oom_size);
1403 my_uint64tos(oom_size_str, info.oom_size, oom_size_len);
1404 static const char oom_size_msg[] = "oom-size";
1405 writer.AddPairData(oom_size_msg, sizeof(oom_size_msg) - 1,
1406 oom_size_str, oom_size_len);
1407 writer.AddBoundary();
1411 if (info.crash_keys) {
1412 CrashKeyStorage::Iterator crash_key_iterator(*info.crash_keys);
1413 const CrashKeyStorage::Entry* entry;
1414 while ((entry = crash_key_iterator.Next())) {
1415 writer.AddPairString(entry->key, entry->value);
1416 writer.AddBoundary();
1421 writer.AddFileContents(g_dump_msg, dump_data, dump_size);
1422 #if defined(ADDRESS_SANITIZER)
1423 // Append a multipart boundary and the contents of the AddressSanitizer log.
1424 writer.AddBoundary();
1425 writer.AddFileContents(g_log_msg, log_data, log_size);
1430 IGNORE_RET(sys_close(temp_file_fd));
1432 #if defined(OS_ANDROID)
1433 if (info.filename) {
1434 int filename_length = my_strlen(info.filename);
1436 // If this was a file, we need to copy it to the right place and use the
1437 // right file name so it gets uploaded by the browser.
1438 const char msg[] = "Output crash dump file:";
1439 WriteLog(msg, sizeof(msg) - 1);
1440 WriteLog(info.filename, filename_length - 1);
1442 char pid_buf[kUint64StringSize];
1443 uint64_t pid_str_length = my_uint64_len(info.pid);
1444 my_uint64tos(pid_buf, info.pid, pid_str_length);
1446 // -1 because we won't need the null terminator on the original filename.
1447 unsigned done_filename_len = filename_length - 1 + pid_str_length;
1448 char* done_filename = reinterpret_cast<char*>(
1449 allocator.Alloc(done_filename_len));
1450 // Rename the file such that the pid is the suffix in order signal to other
1451 // processes that the minidump is complete. The advantage of using the pid
1452 // as the suffix is that it is trivial to associate the minidump with the
1454 // Finally, note strncpy prevents null terminators from
1455 // being copied. Pad the rest with 0's.
1456 my_strncpy(done_filename, info.filename, done_filename_len);
1457 // Append the suffix a null terminator should be added.
1458 my_strncat(done_filename, pid_buf, pid_str_length);
1459 // Rename the minidump file to signal that it is complete.
1460 if (rename(info.filename, done_filename)) {
1461 const char failed_msg[] = "Failed to rename:";
1462 WriteLog(failed_msg, sizeof(failed_msg) - 1);
1463 WriteLog(info.filename, filename_length - 1);
1464 const char to_msg[] = "to";
1465 WriteLog(to_msg, sizeof(to_msg) - 1);
1466 WriteLog(done_filename, done_filename_len - 1);
1474 const pid_t child = sys_fork();
1476 // Spawned helper process.
1478 // This code is called both when a browser is crashing (in which case,
1479 // nothing really matters any more) and when a renderer/plugin crashes, in
1480 // which case we need to continue.
1482 // Since we are a multithreaded app, if we were just to fork(), we might
1483 // grab file descriptors which have just been created in another thread and
1484 // hold them open for too long.
1486 // Thus, we have to loop and try and close everything.
1487 const int fd = sys_open("/proc/self/fd", O_DIRECTORY | O_RDONLY, 0);
1489 for (unsigned i = 3; i < 8192; ++i)
1490 IGNORE_RET(sys_close(i));
1492 google_breakpad::DirectoryReader reader(fd);
1494 while (reader.GetNextEntry(&name)) {
1496 if (my_strtoui(&i, name) && i > 2 && i != fd)
1497 IGNORE_RET(sys_close(i));
1501 IGNORE_RET(sys_close(fd));
1504 IGNORE_RET(sys_setsid());
1506 // Leave one end of a pipe in the upload process and watch for it getting
1507 // closed by the upload process exiting.
1509 if (sys_pipe(fds) >= 0) {
1510 const pid_t upload_child = sys_fork();
1511 if (!upload_child) {
1513 IGNORE_RET(sys_close(fds[0]));
1514 IGNORE_RET(sys_dup2(fds[1], 3));
1515 ExecUploadProcessOrTerminate(info, temp_file, mime_boundary, exe_buf,
1520 if (upload_child > 0) {
1521 IGNORE_RET(sys_close(fds[1]));
1523 const size_t kCrashIdLength = 16;
1524 char id_buf[kCrashIdLength + 1];
1526 WaitForCrashReportUploadProcess(fds[0], kCrashIdLength, id_buf);
1527 HandleCrashReportId(id_buf, bytes_read, kCrashIdLength);
1529 if (sys_waitpid(upload_child, NULL, WNOHANG) == 0) {
1530 // Upload process is still around, kill it.
1531 sys_kill(upload_child, SIGKILL);
1537 IGNORE_RET(sys_unlink(info.filename));
1538 #if defined(ADDRESS_SANITIZER)
1539 IGNORE_RET(sys_unlink(info.log_filename));
1541 IGNORE_RET(sys_unlink(temp_file));
1545 // Main browser process.
1548 (void) HANDLE_EINTR(sys_waitpid(child, NULL, 0));
1551 void InitCrashReporter(const std::string& process_type) {
1552 #if defined(OS_ANDROID)
1553 // This will guarantee that the BuildInfo has been initialized and subsequent
1554 // calls will not require memory allocation.
1555 base::android::BuildInfo::GetInstance();
1557 // Determine the process type and take appropriate action.
1558 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
1559 if (parsed_command_line.HasSwitch(switches::kDisableBreakpad))
1562 if (process_type.empty()) {
1563 bool enable_breakpad = GetCrashReporterClient()->GetCollectStatsConsent() ||
1564 GetCrashReporterClient()->IsRunningUnattended();
1566 !parsed_command_line.HasSwitch(switches::kDisableBreakpad);
1567 if (!enable_breakpad) {
1568 enable_breakpad = parsed_command_line.HasSwitch(
1569 switches::kEnableCrashReporterForTesting);
1571 if (!enable_breakpad) {
1572 VLOG(1) << "Breakpad disabled";
1577 EnableCrashDumping(GetCrashReporterClient()->IsRunningUnattended());
1578 } else if (GetCrashReporterClient()->EnableBreakpadForProcess(process_type)) {
1579 #if defined(OS_ANDROID)
1580 NOTREACHED() << "Breakpad initialized with InitCrashReporter() instead of "
1581 "InitNonBrowserCrashReporter in " << process_type << " process.";
1584 // We might be chrooted in a zygote or renderer process so we cannot call
1585 // GetCollectStatsConsent because that needs access the the user's home
1586 // dir. Instead, we set a command line flag for these processes.
1587 // Even though plugins are not chrooted, we share the same code path for
1589 if (!parsed_command_line.HasSwitch(switches::kEnableCrashReporter))
1592 SetClientIdFromCommandLine(parsed_command_line);
1593 EnableNonBrowserCrashDumping();
1594 VLOG(1) << "Non Browser crash dumping enabled for: " << process_type;
1595 #endif // #if defined(OS_ANDROID)
1598 PostEnableBreakpadInitialization();
1601 #if defined(OS_ANDROID)
1602 void InitNonBrowserCrashReporterForAndroid(const std::string& process_type) {
1603 const CommandLine* command_line = CommandLine::ForCurrentProcess();
1604 if (command_line->HasSwitch(switches::kEnableCrashReporter)) {
1605 // On Android we need to provide a FD to the file where the minidump is
1606 // generated as the renderer and browser run with different UIDs
1607 // (preventing the browser from inspecting the renderer process).
1608 int minidump_fd = base::GlobalDescriptors::GetInstance()->MaybeGet(
1609 GetCrashReporterClient()->GetAndroidMinidumpDescriptor());
1610 if (minidump_fd < 0) {
1611 NOTREACHED() << "Could not find minidump FD, crash reporting disabled.";
1613 EnableNonBrowserCrashDumping(process_type, minidump_fd);
1617 #endif // OS_ANDROID
1619 bool IsCrashReporterEnabled() {
1620 return g_is_crash_reporter_enabled;
1623 } // namespace breakpad