Update V8 to version 4.6.52.
[chromium-blink-merge.git] / components / crash / app / breakpad_linux.cc
blobba57acc96598cfe0ea4482f683ebf21dbddf5183
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"
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>
22 #include <algorithm>
23 #include <string>
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"
46 #if defined(OS_ANDROID)
47 #include <android/log.h>
48 #include <sys/stat.h>
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"
56 #if defined(ADDRESS_SANITIZER)
57 #include <ucontext.h> // for getcontext().
58 #endif
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
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)
74 using crash_reporter::GetCrashReporterClient;
75 using google_breakpad::ExceptionHandler;
76 using google_breakpad::MinidumpDescriptor;
78 namespace breakpad {
80 namespace {
82 #if !defined(OS_CHROMEOS)
83 const char kUploadURL[] = "https://clients2.google.com/cr/report";
84 #endif
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;
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
102 CrashKeyStorage* g_crash_keys = nullptr;
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";
109 for (int i = 15; i >= 0; --i) {
110 output[i] = hextable[v & 15];
111 v >>= 4;
115 // The following helper functions are for calculating uptime.
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;
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;
133 // String buffer size to use to convert a uint64_t to string.
134 const size_t kUint64StringSize = 21;
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;
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;
152 unsigned len = 0;
153 while (i) {
154 len++;
155 i /= 10;
158 return len;
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);
169 #if !defined(OS_CHROMEOS)
170 bool my_isxdigit(char c) {
171 return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'f');
173 #endif
175 size_t LengthWithoutTrailingSpaces(const char* str, size_t len) {
176 while (len > 0 && str[len - 1] == ' ') {
177 len--;
179 return len;
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);
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";
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;
209 MimeWriter(int fd, const char* const mime_boundary);
210 ~MimeWriter();
212 // Append boundary.
213 virtual void AddBoundary();
215 // Append end of file boundary.
216 virtual void AddEnd();
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);
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));
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);
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);
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;
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));
257 void AddItemWithoutTrailingSpaces(const void* base, size_t size);
259 struct kernel_iovec iov_[kIovCapacity];
260 int iov_index_;
262 // Output file descriptor.
263 int fd_;
265 const char* const mime_boundary_;
267 private:
268 DISALLOW_COPY_AND_ASSIGN(MimeWriter);
271 MimeWriter::MimeWriter(int fd, const char* const mime_boundary)
272 : iov_index_(0),
273 fd_(fd),
274 mime_boundary_(mime_boundary) {
277 MimeWriter::~MimeWriter() {
280 void MimeWriter::AddBoundary() {
281 AddString(mime_boundary_);
282 AddString(g_rn);
285 void MimeWriter::AddEnd() {
286 AddString(mime_boundary_);
287 AddString(g_dashdash_msg);
288 AddString(g_rn);
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);
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;
313 unsigned i = 0;
314 size_t done = 0, msg_length = msg_data_size;
316 while (msg_length) {
317 char num[kUint64StringSize];
318 const unsigned num_len = my_uint_len(++i);
319 my_uitos(num, i, num_len);
321 size_t chunk_len = std::min(chunk_size, msg_length);
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);
334 AddString(g_rn);
335 AddBoundary();
336 Flush();
338 done += chunk_len;
339 msg_length -= chunk_len;
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);
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();
360 iov_[iov_index_].iov_base = const_cast<void*>(base);
361 iov_[iov_index_].iov_len = size;
362 ++iov_index_;
365 void MimeWriter::AddItemWithoutTrailingSpaces(const void* base, size_t size) {
366 AddItem(base, LengthWithoutTrailingSpaces(static_cast<const char*>(base),
367 size));
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);
378 void AddBoundary() override;
380 void AddEnd() override;
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;
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;
394 void AddFileContents(const char* filename_msg,
395 uint8_t* file_data,
396 size_t file_size) override;
398 private:
399 DISALLOW_COPY_AND_ASSIGN(CrashReporterWriter);
403 CrashReporterWriter::CrashReporterWriter(int fd) : MimeWriter(fd, "") {}
405 // No-ops.
406 void CrashReporterWriter::AddBoundary() {}
407 void CrashReporterWriter::AddEnd() {}
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);
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();
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;
434 unsigned i = 0;
435 size_t done = 0;
436 size_t msg_length = msg_data_size;
438 while (msg_length) {
439 char num[kUint64StringSize];
440 const unsigned num_len = my_uint_len(++i);
441 my_uitos(num, i, num_len);
443 size_t chunk_len = std::min(chunk_size, msg_length);
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);
452 char data[kUint64StringSize];
453 const unsigned data_len = my_uint_len(write_len);
454 my_uitos(data, write_len, data_len);
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();
464 done += chunk_len;
465 msg_length -= chunk_len;
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);
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();
483 #endif // defined(OS_CHROMEOS)
485 void DumpProcess() {
486 if (g_breakpad)
487 g_breakpad->WriteMinidump();
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
496 #if defined(OS_ANDROID)
497 const char kGoogleBreakpad[] = "google-breakpad";
498 #endif
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
508 size_t WriteNewline() {
509 return WriteLog("\n", 1);
512 #if defined(OS_ANDROID)
513 void AndroidLogWriteHorizontalRule() {
514 __android_log_write(ANDROID_LOG_WARN, kGoogleBreakpad,
515 "### ### ### ### ### ### ### ### ### ### ### ### ###");
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();
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();
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;
554 return false;
556 #endif
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;
569 DCHECK(!minidump.IsFD());
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
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);
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);
614 #endif
616 #if defined(ADDRESS_SANITIZER)
617 extern "C"
618 void __asan_set_error_report_callback(void (*cb)(const char*));
620 extern "C"
621 void AsanLinuxBreakpadCallback(const char* report) {
622 g_asan_report_str = report;
623 // Send minidump here.
624 g_breakpad->SimulateSignalDelivery(SIGKILL);
626 #endif
628 void EnableCrashDumping(bool unattended) {
629 g_is_crash_reporter_enabled = true;
631 base::FilePath tmp_path("/tmp");
632 PathService::Get(base::DIR_TEMP, &tmp_path);
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);
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);
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;
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
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;
689 const bool is_browser_process = (context != nullptr);
690 return FinalizeCrashDoneAndroid(is_browser_process);
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 (!defined(ARCH_CPU_ARMEL) && !defined(ARCH_CPU_ARM64))
698 // TODO(primiano): For the moment microdumps are enabled only on arm (32/64).
699 // Extend support to other architectures (requires some breakpad changes).
700 return;
701 #endif
703 if (!GetCrashReporterClient()->ShouldEnableBreakpadMicrodumps())
704 return;
706 VLOG(1) << "Enabling microdumps crash handler (process_type:"
707 << process_type << ")";
709 // The exception handler runs in a compromised context and cannot use c_str()
710 // as that would require the heap. Therefore, we have to guarantee that the
711 // build fingerprint and product info pointers are always valid.
712 const char* product_name = nullptr;
713 const char* product_version = nullptr;
714 GetCrashReporterClient()->GetProductNameAndVersion(&product_name,
715 &product_version);
717 MinidumpDescriptor descriptor(MinidumpDescriptor::kMicrodumpOnConsole);
719 if (product_name && product_version) {
720 g_microdump_product_info = strdup(
721 (product_name + std::string(":") + product_version).c_str());
722 ANNOTATE_LEAKING_OBJECT_PTR(g_microdump_product_info);
723 descriptor.SetMicrodumpProductInfo(g_microdump_product_info);
726 const char* android_build_fp =
727 base::android::BuildInfo::GetInstance()->android_build_fp();
728 if (android_build_fp) {
729 g_microdump_build_fingerprint = strdup(android_build_fp);
730 ANNOTATE_LEAKING_OBJECT_PTR(g_microdump_build_fingerprint);
731 descriptor.SetMicrodumpBuildFingerprint(g_microdump_build_fingerprint);
734 DCHECK(!g_microdump);
735 bool is_browser_process = process_type.empty() || process_type == "webview";
736 g_microdump = new ExceptionHandler(
737 descriptor,
738 nullptr,
739 MicrodumpCrashDone,
740 reinterpret_cast<void*>(is_browser_process),
741 true, // Install handlers.
742 -1); // Server file descriptor. -1 for in-process.
743 return;
746 bool CrashDoneInProcessNoUpload(
747 const google_breakpad::MinidumpDescriptor& descriptor,
748 void* context,
749 const bool succeeded) {
750 // WARNING: this code runs in a compromised context. It may not call into
751 // libc nor allocate memory normally.
752 if (!succeeded) {
753 static const char msg[] = "Crash dump generation failed.\n";
754 WriteLog(msg, sizeof(msg) - 1);
755 return false;
758 // Start constructing the message to send to the browser.
759 BreakpadInfo info = {0};
760 info.filename = nullptr;
761 info.fd = descriptor.fd();
762 info.process_type = g_process_type;
763 info.process_type_length = my_strlen(g_process_type);
764 info.distro = nullptr;
765 info.distro_length = 0;
766 info.upload = false;
767 info.process_start_time = g_process_start_time;
768 info.pid = g_pid;
769 info.crash_keys = g_crash_keys;
770 HandleCrashDump(info);
771 return FinalizeCrashDoneAndroid(false /* is_browser_process */);
774 void EnableNonBrowserCrashDumping(const std::string& process_type,
775 int minidump_fd) {
776 // This will guarantee that the BuildInfo has been initialized and subsequent
777 // calls will not require memory allocation.
778 base::android::BuildInfo::GetInstance();
779 SetClientIdFromCommandLine(*base::CommandLine::ForCurrentProcess());
781 // On Android, the current sandboxing uses process isolation, in which the
782 // child process runs with a different UID. That breaks the normal crash
783 // reporting where the browser process generates the minidump by inspecting
784 // the child process. This is because the browser process now does not have
785 // the permission to access the states of the child process (as it has a
786 // different UID).
787 // TODO(jcivelli): http://b/issue?id=6776356 we should use a watchdog
788 // process forked from the renderer process that generates the minidump.
789 if (minidump_fd == -1) {
790 LOG(ERROR) << "Minidump file descriptor not found, crash reporting will "
791 " not work.";
792 return;
794 SetProcessStartTime();
795 g_pid = getpid();
797 g_is_crash_reporter_enabled = true;
798 // Save the process type (it is leaked).
799 const size_t process_type_len = process_type.size() + 1;
800 g_process_type = new char[process_type_len];
801 strncpy(g_process_type, process_type.c_str(), process_type_len);
802 new google_breakpad::ExceptionHandler(MinidumpDescriptor(minidump_fd),
803 nullptr, CrashDoneInProcessNoUpload, nullptr, true, -1);
805 #else
806 // Non-Browser = Extension, Gpu, Plugins, Ppapi and Renderer
807 class NonBrowserCrashHandler : public google_breakpad::CrashGenerationClient {
808 public:
809 NonBrowserCrashHandler()
810 : server_fd_(base::GlobalDescriptors::GetInstance()->Get(
811 kCrashDumpSignal)) {
814 ~NonBrowserCrashHandler() override {}
816 bool RequestDump(const void* crash_context,
817 size_t crash_context_size) override {
818 int fds[2] = { -1, -1 };
819 if (sys_socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
820 static const char msg[] = "Failed to create socket for crash dumping.\n";
821 WriteLog(msg, sizeof(msg) - 1);
822 return false;
825 // Start constructing the message to send to the browser.
826 char b; // Dummy variable for sys_read below.
827 const char* b_addr = &b; // Get the address of |b| so we can create the
828 // expected /proc/[pid]/syscall content in the
829 // browser to convert namespace tids.
831 // The length of the control message:
832 static const unsigned kControlMsgSize = sizeof(int);
833 static const unsigned kControlMsgSpaceSize = CMSG_SPACE(kControlMsgSize);
834 static const unsigned kControlMsgLenSize = CMSG_LEN(kControlMsgSize);
836 struct kernel_msghdr msg;
837 my_memset(&msg, 0, sizeof(struct kernel_msghdr));
838 struct kernel_iovec iov[kCrashIovSize];
839 iov[0].iov_base = const_cast<void*>(crash_context);
840 iov[0].iov_len = crash_context_size;
841 iov[1].iov_base = &b_addr;
842 iov[1].iov_len = sizeof(b_addr);
843 iov[2].iov_base = &fds[0];
844 iov[2].iov_len = sizeof(fds[0]);
845 iov[3].iov_base = &g_process_start_time;
846 iov[3].iov_len = sizeof(g_process_start_time);
847 iov[4].iov_base = &base::g_oom_size;
848 iov[4].iov_len = sizeof(base::g_oom_size);
849 google_breakpad::SerializedNonAllocatingMap* serialized_map;
850 iov[5].iov_len = g_crash_keys->Serialize(
851 const_cast<const google_breakpad::SerializedNonAllocatingMap**>(
852 &serialized_map));
853 iov[5].iov_base = serialized_map;
854 #if !defined(ADDRESS_SANITIZER)
855 static_assert(5 == kCrashIovSize - 1, "kCrashIovSize should equal 6");
856 #else
857 iov[6].iov_base = const_cast<char*>(g_asan_report_str);
858 iov[6].iov_len = kMaxAsanReportSize + 1;
859 static_assert(6 == kCrashIovSize - 1, "kCrashIovSize should equal 7");
860 #endif
862 msg.msg_iov = iov;
863 msg.msg_iovlen = kCrashIovSize;
864 char cmsg[kControlMsgSpaceSize];
865 my_memset(cmsg, 0, kControlMsgSpaceSize);
866 msg.msg_control = cmsg;
867 msg.msg_controllen = sizeof(cmsg);
869 struct cmsghdr *hdr = CMSG_FIRSTHDR(&msg);
870 hdr->cmsg_level = SOL_SOCKET;
871 hdr->cmsg_type = SCM_RIGHTS;
872 hdr->cmsg_len = kControlMsgLenSize;
873 ((int*)CMSG_DATA(hdr))[0] = fds[1];
875 if (HANDLE_EINTR(sys_sendmsg(server_fd_, &msg, 0)) < 0) {
876 static const char errmsg[] = "Failed to tell parent about crash.\n";
877 WriteLog(errmsg, sizeof(errmsg) - 1);
878 IGNORE_RET(sys_close(fds[0]));
879 IGNORE_RET(sys_close(fds[1]));
880 return false;
882 IGNORE_RET(sys_close(fds[1]));
884 if (HANDLE_EINTR(sys_read(fds[0], &b, 1)) != 1) {
885 static const char errmsg[] = "Parent failed to complete crash dump.\n";
886 WriteLog(errmsg, sizeof(errmsg) - 1);
888 IGNORE_RET(sys_close(fds[0]));
890 return true;
893 private:
894 // The pipe FD to the browser process, which will handle the crash dumping.
895 const int server_fd_;
897 DISALLOW_COPY_AND_ASSIGN(NonBrowserCrashHandler);
900 void EnableNonBrowserCrashDumping() {
901 g_is_crash_reporter_enabled = true;
902 // We deliberately leak this object.
903 DCHECK(!g_breakpad);
905 g_breakpad = new ExceptionHandler(
906 MinidumpDescriptor("/tmp"), // Unused but needed or Breakpad will assert.
907 nullptr,
908 nullptr,
909 nullptr,
910 true,
911 -1);
912 g_breakpad->set_crash_generation_client(new NonBrowserCrashHandler());
914 #endif // defined(OS_ANDROID)
916 void SetCrashKeyValue(const base::StringPiece& key,
917 const base::StringPiece& value) {
918 g_crash_keys->SetKeyValue(key.data(), value.data());
921 void ClearCrashKey(const base::StringPiece& key) {
922 g_crash_keys->RemoveKey(key.data());
925 // GetCrashReporterClient() cannot call any Set methods until after
926 // InitCrashKeys().
927 void InitCrashKeys() {
928 g_crash_keys = new CrashKeyStorage;
929 GetCrashReporterClient()->RegisterCrashKeys();
930 base::debug::SetCrashKeyReportingFunctions(&SetCrashKeyValue, &ClearCrashKey);
933 // Miscellaneous initialization functions to call after Breakpad has been
934 // enabled.
935 void PostEnableBreakpadInitialization() {
936 SetProcessStartTime();
937 g_pid = getpid();
939 base::debug::SetDumpWithoutCrashingFunction(&DumpProcess);
940 #if defined(ADDRESS_SANITIZER)
941 // Register the callback for AddressSanitizer error reporting.
942 __asan_set_error_report_callback(AsanLinuxBreakpadCallback);
943 #endif
946 } // namespace
948 void LoadDataFromFD(google_breakpad::PageAllocator& allocator,
949 int fd, bool close_fd, uint8_t** file_data, size_t* size) {
950 STAT_STRUCT st;
951 if (FSTAT_FUNC(fd, &st) != 0) {
952 static const char msg[] = "Cannot upload crash dump: stat failed\n";
953 WriteLog(msg, sizeof(msg) - 1);
954 if (close_fd)
955 IGNORE_RET(sys_close(fd));
956 return;
959 *file_data = reinterpret_cast<uint8_t*>(allocator.Alloc(st.st_size));
960 if (!(*file_data)) {
961 static const char msg[] = "Cannot upload crash dump: cannot alloc\n";
962 WriteLog(msg, sizeof(msg) - 1);
963 if (close_fd)
964 IGNORE_RET(sys_close(fd));
965 return;
967 my_memset(*file_data, 0xf, st.st_size);
969 *size = st.st_size;
970 int byte_read = sys_read(fd, *file_data, *size);
971 if (byte_read == -1) {
972 static const char msg[] = "Cannot upload crash dump: read failed\n";
973 WriteLog(msg, sizeof(msg) - 1);
974 if (close_fd)
975 IGNORE_RET(sys_close(fd));
976 return;
979 if (close_fd)
980 IGNORE_RET(sys_close(fd));
983 void LoadDataFromFile(google_breakpad::PageAllocator& allocator,
984 const char* filename,
985 int* fd, uint8_t** file_data, size_t* size) {
986 // WARNING: this code runs in a compromised context. It may not call into
987 // libc nor allocate memory normally.
988 *fd = sys_open(filename, O_RDONLY, 0);
989 *size = 0;
991 if (*fd < 0) {
992 static const char msg[] = "Cannot upload crash dump: failed to open\n";
993 WriteLog(msg, sizeof(msg) - 1);
994 return;
997 LoadDataFromFD(allocator, *fd, true, file_data, size);
1000 // Spawn the appropriate upload process for the current OS:
1001 // - generic Linux invokes wget.
1002 // - ChromeOS invokes crash_reporter.
1003 // |dumpfile| is the path to the dump data file.
1004 // |mime_boundary| is only used on Linux.
1005 // |exe_buf| is only used on CrOS and is the crashing process' name.
1006 void ExecUploadProcessOrTerminate(const BreakpadInfo& info,
1007 const char* dumpfile,
1008 const char* mime_boundary,
1009 const char* exe_buf,
1010 google_breakpad::PageAllocator* allocator) {
1011 #if defined(OS_CHROMEOS)
1012 // CrOS uses crash_reporter instead of wget to report crashes,
1013 // it needs to know where the crash dump lives and the pid and uid of the
1014 // crashing process.
1015 static const char kCrashReporterBinary[] = "/sbin/crash_reporter";
1017 char pid_buf[kUint64StringSize];
1018 uint64_t pid_str_length = my_uint64_len(info.pid);
1019 my_uint64tos(pid_buf, info.pid, pid_str_length);
1020 pid_buf[pid_str_length] = '\0';
1022 char uid_buf[kUint64StringSize];
1023 uid_t uid = geteuid();
1024 uint64_t uid_str_length = my_uint64_len(uid);
1025 my_uint64tos(uid_buf, uid, uid_str_length);
1026 uid_buf[uid_str_length] = '\0';
1028 const char kChromeFlag[] = "--chrome=";
1029 size_t buf_len = my_strlen(dumpfile) + sizeof(kChromeFlag);
1030 char* chrome_flag = reinterpret_cast<char*>(allocator->Alloc(buf_len));
1031 chrome_flag[0] = '\0';
1032 my_strlcat(chrome_flag, kChromeFlag, buf_len);
1033 my_strlcat(chrome_flag, dumpfile, buf_len);
1035 const char kPidFlag[] = "--pid=";
1036 buf_len = my_strlen(pid_buf) + sizeof(kPidFlag);
1037 char* pid_flag = reinterpret_cast<char*>(allocator->Alloc(buf_len));
1038 pid_flag[0] = '\0';
1039 my_strlcat(pid_flag, kPidFlag, buf_len);
1040 my_strlcat(pid_flag, pid_buf, buf_len);
1042 const char kUidFlag[] = "--uid=";
1043 buf_len = my_strlen(uid_buf) + sizeof(kUidFlag);
1044 char* uid_flag = reinterpret_cast<char*>(allocator->Alloc(buf_len));
1045 uid_flag[0] = '\0';
1046 my_strlcat(uid_flag, kUidFlag, buf_len);
1047 my_strlcat(uid_flag, uid_buf, buf_len);
1049 const char kExeBuf[] = "--exe=";
1050 buf_len = my_strlen(exe_buf) + sizeof(kExeBuf);
1051 char* exe_flag = reinterpret_cast<char*>(allocator->Alloc(buf_len));
1052 exe_flag[0] = '\0';
1053 my_strlcat(exe_flag, kExeBuf, buf_len);
1054 my_strlcat(exe_flag, exe_buf, buf_len);
1056 const char* args[] = {
1057 kCrashReporterBinary,
1058 chrome_flag,
1059 pid_flag,
1060 uid_flag,
1061 exe_flag,
1062 nullptr,
1064 static const char msg[] = "Cannot upload crash dump: cannot exec "
1065 "/sbin/crash_reporter\n";
1066 #else
1067 // Compress |dumpfile| with gzip.
1068 const pid_t gzip_child = sys_fork();
1069 if (gzip_child < 0) {
1070 static const char msg[] = "sys_fork() for gzip process failed.\n";
1071 WriteLog(msg, sizeof(msg) - 1);
1072 sys__exit(1);
1074 if (!gzip_child) {
1075 // gzip process.
1076 const char* args[] = {
1077 "/bin/gzip",
1078 "-f", // Do not prompt to verify before overwriting.
1079 dumpfile,
1080 nullptr,
1082 execve(args[0], const_cast<char**>(args), environ);
1083 static const char msg[] = "Cannot exec gzip.\n";
1084 WriteLog(msg, sizeof(msg) - 1);
1085 sys__exit(1);
1087 // Wait for gzip process.
1088 int status = 0;
1089 if (sys_waitpid(gzip_child, &status, 0) != gzip_child ||
1090 !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
1091 static const char msg[] = "sys_waitpid() for gzip process failed.\n";
1092 WriteLog(msg, sizeof(msg) - 1);
1093 sys_kill(gzip_child, SIGKILL);
1094 sys__exit(1);
1097 static const char kGzipExtension[] = ".gz";
1098 const size_t gzip_file_size = my_strlen(dumpfile) + sizeof(kGzipExtension);
1099 char* const gzip_file = reinterpret_cast<char*>(allocator->Alloc(
1100 gzip_file_size));
1101 my_strlcpy(gzip_file, dumpfile, gzip_file_size);
1102 my_strlcat(gzip_file, kGzipExtension, gzip_file_size);
1104 // Rename |gzip_file| to |dumpfile| (the original file was deleted by gzip).
1105 if (rename(gzip_file, dumpfile)) {
1106 static const char msg[] = "Failed to rename gzipped file.\n";
1107 WriteLog(msg, sizeof(msg) - 1);
1108 sys__exit(1);
1111 // The --header argument to wget looks like:
1112 // --header=Content-Encoding: gzip
1113 // --header=Content-Type: multipart/form-data; boundary=XYZ
1114 // where the boundary has two fewer leading '-' chars
1115 static const char header_content_encoding[] =
1116 "--header=Content-Encoding: gzip";
1117 static const char header_msg[] =
1118 "--header=Content-Type: multipart/form-data; boundary=";
1119 const size_t header_content_type_size =
1120 sizeof(header_msg) - 1 + my_strlen(mime_boundary) - 2 + 1;
1121 char* const header_content_type = reinterpret_cast<char*>(allocator->Alloc(
1122 header_content_type_size));
1123 my_strlcpy(header_content_type, header_msg, header_content_type_size);
1124 my_strlcat(header_content_type, mime_boundary + 2, header_content_type_size);
1126 // The --post-file argument to wget looks like:
1127 // --post-file=/tmp/...
1128 static const char post_file_msg[] = "--post-file=";
1129 const size_t post_file_size =
1130 sizeof(post_file_msg) - 1 + my_strlen(dumpfile) + 1;
1131 char* const post_file = reinterpret_cast<char*>(allocator->Alloc(
1132 post_file_size));
1133 my_strlcpy(post_file, post_file_msg, post_file_size);
1134 my_strlcat(post_file, dumpfile, post_file_size);
1136 static const char kWgetBinary[] = "/usr/bin/wget";
1137 const char* args[] = {
1138 kWgetBinary,
1139 header_content_encoding,
1140 header_content_type,
1141 post_file,
1142 kUploadURL,
1143 "--timeout=10", // Set a timeout so we don't hang forever.
1144 "--tries=1", // Don't retry if the upload fails.
1145 "-O", // output reply to fd 3
1146 "/dev/fd/3",
1147 nullptr,
1149 static const char msg[] = "Cannot upload crash dump: cannot exec "
1150 "/usr/bin/wget\n";
1151 #endif
1152 execve(args[0], const_cast<char**>(args), environ);
1153 WriteLog(msg, sizeof(msg) - 1);
1154 sys__exit(1);
1157 // Runs in the helper process to wait for the upload process running
1158 // ExecUploadProcessOrTerminate() to finish. Returns the number of bytes written
1159 // to |fd| and save the written contents to |buf|.
1160 // |buf| needs to be big enough to hold |bytes_to_read| + 1 characters.
1161 size_t WaitForCrashReportUploadProcess(int fd, size_t bytes_to_read,
1162 char* buf) {
1163 size_t bytes_read = 0;
1165 // Upload should finish in about 10 seconds. Add a few more 500 ms
1166 // internals to account for process startup time.
1167 for (size_t wait_count = 0; wait_count < 24; ++wait_count) {
1168 struct kernel_pollfd poll_fd;
1169 poll_fd.fd = fd;
1170 poll_fd.events = POLLIN | POLLPRI | POLLERR;
1171 int ret = sys_poll(&poll_fd, 1, 500);
1172 if (ret < 0) {
1173 // Error
1174 break;
1175 } else if (ret > 0) {
1176 // There is data to read.
1177 ssize_t len = HANDLE_EINTR(
1178 sys_read(fd, buf + bytes_read, bytes_to_read - bytes_read));
1179 if (len < 0)
1180 break;
1181 bytes_read += len;
1182 if (bytes_read == bytes_to_read)
1183 break;
1185 // |ret| == 0 -> timed out, continue waiting.
1186 // or |bytes_read| < |bytes_to_read| still, keep reading.
1188 buf[bytes_to_read] = 0; // Always NUL terminate the buffer.
1189 return bytes_read;
1192 // |buf| should be |expected_len| + 1 characters in size and nullptr terminated.
1193 bool IsValidCrashReportId(const char* buf, size_t bytes_read,
1194 size_t expected_len) {
1195 if (bytes_read != expected_len)
1196 return false;
1197 #if defined(OS_CHROMEOS)
1198 return my_strcmp(buf, "_sys_cr_finished") == 0;
1199 #else
1200 for (size_t i = 0; i < bytes_read; ++i) {
1201 if (!my_isxdigit(buf[i]))
1202 return false;
1204 return true;
1205 #endif
1208 // |buf| should be |expected_len| + 1 characters in size and nullptr terminated.
1209 void HandleCrashReportId(const char* buf, size_t bytes_read,
1210 size_t expected_len) {
1211 WriteNewline();
1212 if (!IsValidCrashReportId(buf, bytes_read, expected_len)) {
1213 #if defined(OS_CHROMEOS)
1214 static const char msg[] =
1215 "System crash-reporter failed to process crash report.";
1216 #else
1217 static const char msg[] = "Failed to get crash dump id.";
1218 #endif
1219 WriteLog(msg, sizeof(msg) - 1);
1220 WriteNewline();
1222 static const char id_msg[] = "Report Id: ";
1223 WriteLog(id_msg, sizeof(id_msg) - 1);
1224 WriteLog(buf, bytes_read);
1225 WriteNewline();
1226 return;
1229 #if defined(OS_CHROMEOS)
1230 static const char msg[] = "Crash dump received by crash_reporter\n";
1231 WriteLog(msg, sizeof(msg) - 1);
1232 #else
1233 // Write crash dump id to stderr.
1234 static const char msg[] = "Crash dump id: ";
1235 WriteLog(msg, sizeof(msg) - 1);
1236 WriteLog(buf, my_strlen(buf));
1237 WriteNewline();
1239 // Write crash dump id to crash log as: seconds_since_epoch,crash_id
1240 struct kernel_timeval tv;
1241 if (g_crash_log_path && !sys_gettimeofday(&tv, nullptr)) {
1242 uint64_t time = kernel_timeval_to_ms(&tv) / 1000;
1243 char time_str[kUint64StringSize];
1244 const unsigned time_len = my_uint64_len(time);
1245 my_uint64tos(time_str, time, time_len);
1247 const int kLogOpenFlags = O_CREAT | O_WRONLY | O_APPEND | O_CLOEXEC;
1248 int log_fd = sys_open(g_crash_log_path, kLogOpenFlags, 0600);
1249 if (log_fd > 0) {
1250 sys_write(log_fd, time_str, time_len);
1251 sys_write(log_fd, ",", 1);
1252 sys_write(log_fd, buf, my_strlen(buf));
1253 sys_write(log_fd, "\n", 1);
1254 IGNORE_RET(sys_close(log_fd));
1257 #endif
1260 #if defined(OS_CHROMEOS)
1261 const char* GetCrashingProcessName(const BreakpadInfo& info,
1262 google_breakpad::PageAllocator* allocator) {
1263 // Symlink to process binary is at /proc/###/exe.
1264 char linkpath[kUint64StringSize + sizeof("/proc/") + sizeof("/exe")] =
1265 "/proc/";
1266 uint64_t pid_value_len = my_uint64_len(info.pid);
1267 my_uint64tos(linkpath + sizeof("/proc/") - 1, info.pid, pid_value_len);
1268 linkpath[sizeof("/proc/") - 1 + pid_value_len] = '\0';
1269 my_strlcat(linkpath, "/exe", sizeof(linkpath));
1271 const int kMaxSize = 4096;
1272 char* link = reinterpret_cast<char*>(allocator->Alloc(kMaxSize));
1273 if (link) {
1274 ssize_t size = readlink(linkpath, link, kMaxSize);
1275 if (size < kMaxSize && size > 0) {
1276 // readlink(2) doesn't add a terminating NUL, so do it now.
1277 link[size] = '\0';
1279 const char* name = my_strrchr(link, '/');
1280 if (name)
1281 return name + 1;
1282 return link;
1285 // Either way too long, or a read error.
1286 return "chrome-crash-unknown-process";
1288 #endif
1290 void HandleCrashDump(const BreakpadInfo& info) {
1291 int dumpfd;
1292 bool keep_fd = false;
1293 size_t dump_size;
1294 uint8_t* dump_data;
1295 google_breakpad::PageAllocator allocator;
1296 const char* exe_buf = nullptr;
1298 if (GetCrashReporterClient()->HandleCrashDump(info.filename)) {
1299 return;
1302 #if defined(OS_CHROMEOS)
1303 // Grab the crashing process' name now, when it should still be available.
1304 // If we try to do this later in our grandchild the crashing process has
1305 // already terminated.
1306 exe_buf = GetCrashingProcessName(info, &allocator);
1307 #endif
1309 if (info.fd != -1) {
1310 // Dump is provided with an open FD.
1311 keep_fd = true;
1312 dumpfd = info.fd;
1314 // The FD is pointing to the end of the file.
1315 // Rewind, we'll read the data next.
1316 if (lseek(dumpfd, 0, SEEK_SET) == -1) {
1317 static const char msg[] = "Cannot upload crash dump: failed to "
1318 "reposition minidump FD\n";
1319 WriteLog(msg, sizeof(msg) - 1);
1320 IGNORE_RET(sys_close(dumpfd));
1321 return;
1323 LoadDataFromFD(allocator, info.fd, false, &dump_data, &dump_size);
1324 } else {
1325 // Dump is provided with a path.
1326 keep_fd = false;
1327 LoadDataFromFile(allocator, info.filename, &dumpfd, &dump_data, &dump_size);
1330 // TODO(jcivelli): make log work when using FDs.
1331 #if defined(ADDRESS_SANITIZER)
1332 int logfd;
1333 size_t log_size;
1334 uint8_t* log_data;
1335 // Load the AddressSanitizer log into log_data.
1336 LoadDataFromFile(allocator, info.log_filename, &logfd, &log_data, &log_size);
1337 #endif
1339 // We need to build a MIME block for uploading to the server. Since we are
1340 // going to fork and run wget, it needs to be written to a temp file.
1341 const int ufd = sys_open("/dev/urandom", O_RDONLY, 0);
1342 if (ufd < 0) {
1343 static const char msg[] = "Cannot upload crash dump because /dev/urandom"
1344 " is missing\n";
1345 WriteLog(msg, sizeof(msg) - 1);
1346 return;
1349 static const char temp_file_template[] =
1350 "/tmp/chromium-upload-XXXXXXXXXXXXXXXX";
1351 char temp_file[sizeof(temp_file_template)];
1352 int temp_file_fd = -1;
1353 if (keep_fd) {
1354 temp_file_fd = dumpfd;
1355 // Rewind the destination, we are going to overwrite it.
1356 if (lseek(dumpfd, 0, SEEK_SET) == -1) {
1357 static const char msg[] = "Cannot upload crash dump: failed to "
1358 "reposition minidump FD (2)\n";
1359 WriteLog(msg, sizeof(msg) - 1);
1360 IGNORE_RET(sys_close(dumpfd));
1361 return;
1363 } else {
1364 if (info.upload) {
1365 my_memcpy(temp_file, temp_file_template, sizeof(temp_file_template));
1367 for (unsigned i = 0; i < 10; ++i) {
1368 uint64_t t;
1369 sys_read(ufd, &t, sizeof(t));
1370 write_uint64_hex(temp_file + sizeof(temp_file) - (16 + 1), t);
1372 temp_file_fd = sys_open(temp_file, O_WRONLY | O_CREAT | O_EXCL, 0600);
1373 if (temp_file_fd >= 0)
1374 break;
1377 if (temp_file_fd < 0) {
1378 static const char msg[] = "Failed to create temporary file in /tmp: "
1379 "cannot upload crash dump\n";
1380 WriteLog(msg, sizeof(msg) - 1);
1381 IGNORE_RET(sys_close(ufd));
1382 return;
1384 } else {
1385 temp_file_fd = sys_open(info.filename, O_WRONLY, 0600);
1386 if (temp_file_fd < 0) {
1387 static const char msg[] = "Failed to save crash dump: failed to open\n";
1388 WriteLog(msg, sizeof(msg) - 1);
1389 IGNORE_RET(sys_close(ufd));
1390 return;
1395 // The MIME boundary is 28 hyphens, followed by a 64-bit nonce and a NUL.
1396 char mime_boundary[28 + 16 + 1];
1397 my_memset(mime_boundary, '-', 28);
1398 uint64_t boundary_rand;
1399 sys_read(ufd, &boundary_rand, sizeof(boundary_rand));
1400 write_uint64_hex(mime_boundary + 28, boundary_rand);
1401 mime_boundary[28 + 16] = 0;
1402 IGNORE_RET(sys_close(ufd));
1404 // The MIME block looks like this:
1405 // BOUNDARY \r\n
1406 // Content-Disposition: form-data; name="prod" \r\n \r\n
1407 // Chrome_Linux \r\n
1408 // BOUNDARY \r\n
1409 // Content-Disposition: form-data; name="ver" \r\n \r\n
1410 // 1.2.3.4 \r\n
1411 // BOUNDARY \r\n
1413 // zero or one:
1414 // Content-Disposition: form-data; name="ptime" \r\n \r\n
1415 // abcdef \r\n
1416 // BOUNDARY \r\n
1418 // zero or one:
1419 // Content-Disposition: form-data; name="ptype" \r\n \r\n
1420 // abcdef \r\n
1421 // BOUNDARY \r\n
1423 // zero or one:
1424 // Content-Disposition: form-data; name="lsb-release" \r\n \r\n
1425 // abcdef \r\n
1426 // BOUNDARY \r\n
1428 // zero or one:
1429 // Content-Disposition: form-data; name="oom-size" \r\n \r\n
1430 // 1234567890 \r\n
1431 // BOUNDARY \r\n
1433 // zero or more (up to CrashKeyStorage::num_entries = 64):
1434 // Content-Disposition: form-data; name=crash-key-name \r\n
1435 // crash-key-value \r\n
1436 // BOUNDARY \r\n
1438 // Content-Disposition: form-data; name="dump"; filename="dump" \r\n
1439 // Content-Type: application/octet-stream \r\n \r\n
1440 // <dump contents>
1441 // \r\n BOUNDARY -- \r\n
1443 #if defined(OS_CHROMEOS)
1444 CrashReporterWriter writer(temp_file_fd);
1445 #else
1446 MimeWriter writer(temp_file_fd, mime_boundary);
1447 #endif
1449 const char* product_name = "";
1450 const char* version = "";
1452 GetCrashReporterClient()->GetProductNameAndVersion(&product_name, &version);
1454 writer.AddBoundary();
1455 writer.AddPairString("prod", product_name);
1456 writer.AddBoundary();
1457 writer.AddPairString("ver", version);
1458 writer.AddBoundary();
1459 if (info.pid > 0) {
1460 char pid_value_buf[kUint64StringSize];
1461 uint64_t pid_value_len = my_uint64_len(info.pid);
1462 my_uint64tos(pid_value_buf, info.pid, pid_value_len);
1463 static const char pid_key_name[] = "pid";
1464 writer.AddPairData(pid_key_name, sizeof(pid_key_name) - 1,
1465 pid_value_buf, pid_value_len);
1466 writer.AddBoundary();
1468 #if defined(OS_ANDROID)
1469 // Addtional MIME blocks are added for logging on Android devices.
1470 static const char android_build_id[] = "android_build_id";
1471 static const char android_build_fp[] = "android_build_fp";
1472 static const char device[] = "device";
1473 static const char model[] = "model";
1474 static const char brand[] = "brand";
1475 static const char exception_info[] = "exception_info";
1477 base::android::BuildInfo* android_build_info =
1478 base::android::BuildInfo::GetInstance();
1479 writer.AddPairString(
1480 android_build_id, android_build_info->android_build_id());
1481 writer.AddBoundary();
1482 writer.AddPairString(
1483 android_build_fp, android_build_info->android_build_fp());
1484 writer.AddBoundary();
1485 writer.AddPairString(device, android_build_info->device());
1486 writer.AddBoundary();
1487 writer.AddPairString(model, android_build_info->model());
1488 writer.AddBoundary();
1489 writer.AddPairString(brand, android_build_info->brand());
1490 writer.AddBoundary();
1491 if (android_build_info->java_exception_info() != nullptr) {
1492 writer.AddPairString(exception_info,
1493 android_build_info->java_exception_info());
1494 writer.AddBoundary();
1496 #endif
1497 writer.Flush();
1500 if (info.process_start_time > 0) {
1501 struct kernel_timeval tv;
1502 if (!sys_gettimeofday(&tv, nullptr)) {
1503 uint64_t time = kernel_timeval_to_ms(&tv);
1504 if (time > info.process_start_time) {
1505 time -= info.process_start_time;
1506 char time_str[kUint64StringSize];
1507 const unsigned time_len = my_uint64_len(time);
1508 my_uint64tos(time_str, time, time_len);
1510 static const char process_time_msg[] = "ptime";
1511 writer.AddPairData(process_time_msg, sizeof(process_time_msg) - 1,
1512 time_str, time_len);
1513 writer.AddBoundary();
1514 writer.Flush();
1519 if (info.process_type_length) {
1520 writer.AddPairString("ptype", info.process_type);
1521 writer.AddBoundary();
1522 writer.Flush();
1525 if (info.distro_length) {
1526 static const char distro_msg[] = "lsb-release";
1527 writer.AddPairString(distro_msg, info.distro);
1528 writer.AddBoundary();
1529 writer.Flush();
1532 if (info.oom_size) {
1533 char oom_size_str[kUint64StringSize];
1534 const unsigned oom_size_len = my_uint64_len(info.oom_size);
1535 my_uint64tos(oom_size_str, info.oom_size, oom_size_len);
1536 static const char oom_size_msg[] = "oom-size";
1537 writer.AddPairData(oom_size_msg, sizeof(oom_size_msg) - 1,
1538 oom_size_str, oom_size_len);
1539 writer.AddBoundary();
1540 writer.Flush();
1543 if (info.crash_keys) {
1544 CrashKeyStorage::Iterator crash_key_iterator(*info.crash_keys);
1545 const CrashKeyStorage::Entry* entry;
1546 while ((entry = crash_key_iterator.Next())) {
1547 writer.AddPairString(entry->key, entry->value);
1548 writer.AddBoundary();
1549 writer.Flush();
1553 writer.AddFileContents(g_dump_msg, dump_data, dump_size);
1554 #if defined(ADDRESS_SANITIZER)
1555 // Append a multipart boundary and the contents of the AddressSanitizer log.
1556 writer.AddBoundary();
1557 writer.AddFileContents(g_log_msg, log_data, log_size);
1558 #endif
1559 writer.AddEnd();
1560 writer.Flush();
1562 IGNORE_RET(sys_close(temp_file_fd));
1564 #if defined(OS_ANDROID)
1565 if (info.filename) {
1566 size_t filename_length = my_strlen(info.filename);
1568 // If this was a file, we need to copy it to the right place and use the
1569 // right file name so it gets uploaded by the browser.
1570 const char msg[] = "Output crash dump file:";
1571 WriteLog(msg, sizeof(msg) - 1);
1572 WriteLog(info.filename, filename_length);
1574 char pid_buf[kUint64StringSize];
1575 size_t pid_str_length = my_uint64_len(info.pid);
1576 my_uint64tos(pid_buf, info.pid, pid_str_length);
1577 pid_buf[pid_str_length] = 0; // my_uint64tos() doesn't null-terminate.
1579 size_t done_filename_len = filename_length + pid_str_length + 1;
1580 char* done_filename = reinterpret_cast<char*>(
1581 allocator.Alloc(done_filename_len));
1582 // Rename the file such that the pid is the suffix in order signal to other
1583 // processes that the minidump is complete. The advantage of using the pid
1584 // as the suffix is that it is trivial to associate the minidump with the
1585 // crashed process.
1586 my_strlcpy(done_filename, info.filename, done_filename_len);
1587 my_strlcat(done_filename, pid_buf, done_filename_len);
1588 // Rename the minidump file to signal that it is complete.
1589 if (rename(info.filename, done_filename)) {
1590 const char failed_msg[] = "Failed to rename:";
1591 WriteLog(failed_msg, sizeof(failed_msg) - 1);
1592 WriteLog(info.filename, filename_length);
1593 const char to_msg[] = "to";
1594 WriteLog(to_msg, sizeof(to_msg) - 1);
1595 WriteLog(done_filename, done_filename_len - 1);
1598 #endif
1600 if (!info.upload)
1601 return;
1603 const pid_t child = sys_fork();
1604 if (!child) {
1605 // Spawned helper process.
1607 // This code is called both when a browser is crashing (in which case,
1608 // nothing really matters any more) and when a renderer/plugin crashes, in
1609 // which case we need to continue.
1611 // Since we are a multithreaded app, if we were just to fork(), we might
1612 // grab file descriptors which have just been created in another thread and
1613 // hold them open for too long.
1615 // Thus, we have to loop and try and close everything.
1616 const int fd = sys_open("/proc/self/fd", O_DIRECTORY | O_RDONLY, 0);
1617 if (fd < 0) {
1618 for (unsigned i = 3; i < 8192; ++i)
1619 IGNORE_RET(sys_close(i));
1620 } else {
1621 google_breakpad::DirectoryReader reader(fd);
1622 const char* name;
1623 while (reader.GetNextEntry(&name)) {
1624 int i;
1625 if (my_strtoui(&i, name) && i > 2 && i != fd)
1626 IGNORE_RET(sys_close(i));
1627 reader.PopEntry();
1630 IGNORE_RET(sys_close(fd));
1633 IGNORE_RET(sys_setsid());
1635 // Leave one end of a pipe in the upload process and watch for it getting
1636 // closed by the upload process exiting.
1637 int fds[2];
1638 if (sys_pipe(fds) >= 0) {
1639 const pid_t upload_child = sys_fork();
1640 if (!upload_child) {
1641 // Upload process.
1642 IGNORE_RET(sys_close(fds[0]));
1643 IGNORE_RET(sys_dup2(fds[1], 3));
1644 ExecUploadProcessOrTerminate(info, temp_file, mime_boundary, exe_buf,
1645 &allocator);
1648 // Helper process.
1649 if (upload_child > 0) {
1650 IGNORE_RET(sys_close(fds[1]));
1652 const size_t kCrashIdLength = 16;
1653 char id_buf[kCrashIdLength + 1];
1654 size_t bytes_read =
1655 WaitForCrashReportUploadProcess(fds[0], kCrashIdLength, id_buf);
1656 HandleCrashReportId(id_buf, bytes_read, kCrashIdLength);
1658 if (sys_waitpid(upload_child, nullptr, WNOHANG) == 0) {
1659 // Upload process is still around, kill it.
1660 sys_kill(upload_child, SIGKILL);
1665 // Helper process.
1666 IGNORE_RET(sys_unlink(info.filename));
1667 #if defined(ADDRESS_SANITIZER)
1668 IGNORE_RET(sys_unlink(info.log_filename));
1669 #endif
1670 IGNORE_RET(sys_unlink(temp_file));
1671 sys__exit(0);
1674 // Main browser process.
1675 if (child <= 0)
1676 return;
1677 (void) HANDLE_EINTR(sys_waitpid(child, nullptr, 0));
1680 void InitCrashReporter(const std::string& process_type) {
1681 #if defined(OS_ANDROID)
1682 // This will guarantee that the BuildInfo has been initialized and subsequent
1683 // calls will not require memory allocation.
1684 base::android::BuildInfo::GetInstance();
1686 // Handler registration is LIFO. Install the microdump handler first, such
1687 // that if conventional minidump crash reporting is enabled below, it takes
1688 // precedence (i.e. its handler is run first) over the microdump handler.
1689 InitMicrodumpCrashHandlerIfNecessary(process_type);
1690 #endif
1691 // Determine the process type and take appropriate action.
1692 const base::CommandLine& parsed_command_line =
1693 *base::CommandLine::ForCurrentProcess();
1694 if (parsed_command_line.HasSwitch(switches::kDisableBreakpad))
1695 return;
1697 if (process_type.empty()) {
1698 bool enable_breakpad = GetCrashReporterClient()->GetCollectStatsConsent() ||
1699 GetCrashReporterClient()->IsRunningUnattended();
1700 enable_breakpad &=
1701 !parsed_command_line.HasSwitch(switches::kDisableBreakpad);
1702 if (!enable_breakpad) {
1703 enable_breakpad = parsed_command_line.HasSwitch(
1704 switches::kEnableCrashReporterForTesting);
1706 if (!enable_breakpad) {
1707 VLOG(1) << "Breakpad disabled";
1708 return;
1711 InitCrashKeys();
1712 EnableCrashDumping(GetCrashReporterClient()->IsRunningUnattended());
1713 } else if (GetCrashReporterClient()->EnableBreakpadForProcess(process_type)) {
1714 #if defined(OS_ANDROID)
1715 NOTREACHED() << "Breakpad initialized with InitCrashReporter() instead of "
1716 "InitNonBrowserCrashReporter in " << process_type << " process.";
1717 return;
1718 #else
1719 // We might be chrooted in a zygote or renderer process so we cannot call
1720 // GetCollectStatsConsent because that needs access the the user's home
1721 // dir. Instead, we set a command line flag for these processes.
1722 // Even though plugins are not chrooted, we share the same code path for
1723 // simplicity.
1724 if (!parsed_command_line.HasSwitch(switches::kEnableCrashReporter))
1725 return;
1726 InitCrashKeys();
1727 SetClientIdFromCommandLine(parsed_command_line);
1728 EnableNonBrowserCrashDumping();
1729 VLOG(1) << "Non Browser crash dumping enabled for: " << process_type;
1730 #endif // #if defined(OS_ANDROID)
1733 PostEnableBreakpadInitialization();
1736 #if defined(OS_ANDROID)
1737 void InitNonBrowserCrashReporterForAndroid(const std::string& process_type) {
1738 const base::CommandLine* command_line =
1739 base::CommandLine::ForCurrentProcess();
1741 // Handler registration is LIFO. Install the microdump handler first, such
1742 // that if conventional minidump crash reporting is enabled below, it takes
1743 // precedence (i.e. its handler is run first) over the microdump handler.
1744 InitMicrodumpCrashHandlerIfNecessary(process_type);
1746 if (command_line->HasSwitch(switches::kEnableCrashReporter)) {
1747 // On Android we need to provide a FD to the file where the minidump is
1748 // generated as the renderer and browser run with different UIDs
1749 // (preventing the browser from inspecting the renderer process).
1750 int minidump_fd = base::GlobalDescriptors::GetInstance()->MaybeGet(
1751 GetCrashReporterClient()->GetAndroidMinidumpDescriptor());
1752 if (minidump_fd < 0) {
1753 NOTREACHED() << "Could not find minidump FD, crash reporting disabled.";
1754 } else {
1755 InitCrashKeys();
1756 EnableNonBrowserCrashDumping(process_type, minidump_fd);
1760 #endif // OS_ANDROID
1762 bool IsCrashReporterEnabled() {
1763 return g_is_crash_reporter_enabled;
1766 } // namespace breakpad