aboutsummaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorbenchan@chromium.org <benchan@chromium.org@4c0a9323-5329-0410-9bdc-e9ce6186880e>2012-01-07 02:25:22 +0000
committerbenchan@chromium.org <benchan@chromium.org@4c0a9323-5329-0410-9bdc-e9ce6186880e>2012-01-07 02:25:22 +0000
commit07e521c396be01162b4cb8119954401b7eaa510c (patch)
tree8af73fa62c70581555d2e8e3c1abf0d708e78f3d /src/common
parentSend crash dumps to Google via HTTPS instead of HTTP, since they might (diff)
downloadbreakpad-07e521c396be01162b4cb8119954401b7eaa510c.tar.xz
Add utilities for processing Linux core dump files.
This patch is part of a bigger patch that helps merging the breakpad code with the modified version in Chromium OS. Specifically, this patch makes the following changes: 1. Add an ElfCoreDump class for processing Linux core dump files, which will later be used to implement the core dump to minidump conversion. 2. Add a CrashGenerator class for generating a crash with a core dump file for testing the functionalities of ElfCoreDump. 3. Move some utility functions for reading/writing files to file_utils.h. BUG=455 TEST=Tested the following: 1. Build on 32-bit and 64-bit Linux with gcc 4.4.3 and gcc 4.6. 2. Build on Mac OS X 10.6.8 with gcc 4.2 and clang 3.0 (with latest gmock). 3. All unit tests pass. Review URL: http://breakpad.appspot.com/337001 git-svn-id: http://google-breakpad.googlecode.com/svn/trunk@900 4c0a9323-5329-0410-9bdc-e9ce6186880e
Diffstat (limited to 'src/common')
-rw-r--r--src/common/linux/elf_core_dump.cc179
-rw-r--r--src/common/linux/elf_core_dump.h153
-rw-r--r--src/common/linux/elf_core_dump_unittest.cc238
-rw-r--r--src/common/linux/memory_mapped_file_unittest.cc29
-rw-r--r--src/common/linux/tests/crash_generator.cc206
-rw-r--r--src/common/linux/tests/crash_generator.h108
-rw-r--r--src/common/tests/auto_tempdir.h6
-rw-r--r--src/common/tests/file_utils.cc97
-rw-r--r--src/common/tests/file_utils.h49
9 files changed, 1039 insertions, 26 deletions
diff --git a/src/common/linux/elf_core_dump.cc b/src/common/linux/elf_core_dump.cc
new file mode 100644
index 00000000..0e7db7b1
--- /dev/null
+++ b/src/common/linux/elf_core_dump.cc
@@ -0,0 +1,179 @@
+// Copyright (c) 2011, Google Inc.
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// elf_core_dump.cc: Implement google_breakpad::ElfCoreDump.
+// See elf_core_dump.h for details.
+
+#include "common/linux/elf_core_dump.h"
+
+#include <stddef.h>
+#include <string.h>
+
+namespace google_breakpad {
+
+// Implementation of ElfCoreDump::Note.
+
+ElfCoreDump::Note::Note() {}
+
+ElfCoreDump::Note::Note(const MemoryRange& content) : content_(content) {}
+
+bool ElfCoreDump::Note::IsValid() const {
+ return GetHeader() != NULL;
+}
+
+const ElfCoreDump::Nhdr* ElfCoreDump::Note::GetHeader() const {
+ return content_.GetData<Nhdr>(0);
+}
+
+ElfCoreDump::Word ElfCoreDump::Note::GetType() const {
+ const Nhdr* header = GetHeader();
+ // 0 is not being used as a NOTE type.
+ return header ? header->n_type : 0;
+}
+
+MemoryRange ElfCoreDump::Note::GetName() const {
+ const Nhdr* header = GetHeader();
+ if (header) {
+ return content_.Subrange(sizeof(Nhdr), header->n_namesz);
+ }
+ return MemoryRange();
+}
+
+MemoryRange ElfCoreDump::Note::GetDescription() const {
+ const Nhdr* header = GetHeader();
+ if (header) {
+ return content_.Subrange(AlignedSize(sizeof(Nhdr) + header->n_namesz),
+ header->n_descsz);
+ }
+ return MemoryRange();
+}
+
+ElfCoreDump::Note ElfCoreDump::Note::GetNextNote() const {
+ MemoryRange next_content;
+ const Nhdr* header = GetHeader();
+ if (header) {
+ size_t next_offset = AlignedSize(sizeof(Nhdr) + header->n_namesz);
+ next_offset = AlignedSize(next_offset + header->n_descsz);
+ next_content =
+ content_.Subrange(next_offset, content_.length() - next_offset);
+ }
+ return Note(next_content);
+}
+
+// static
+size_t ElfCoreDump::Note::AlignedSize(size_t size) {
+ size_t mask = sizeof(Word) - 1;
+ return (size + mask) & ~mask;
+}
+
+
+// Implementation of ElfCoreDump.
+
+ElfCoreDump::ElfCoreDump() {}
+
+ElfCoreDump::ElfCoreDump(const MemoryRange& content)
+ : content_(content) {
+}
+
+void ElfCoreDump::SetContent(const MemoryRange& content) {
+ content_ = content;
+}
+
+bool ElfCoreDump::IsValid() const {
+ const Ehdr* header = GetHeader();
+ return (header &&
+ header->e_ident[0] == ELFMAG0 &&
+ header->e_ident[1] == ELFMAG1 &&
+ header->e_ident[2] == ELFMAG2 &&
+ header->e_ident[3] == ELFMAG3 &&
+ header->e_ident[4] == kClass &&
+ header->e_version == EV_CURRENT &&
+ header->e_type == ET_CORE);
+}
+
+const ElfCoreDump::Ehdr* ElfCoreDump::GetHeader() const {
+ return content_.GetData<Ehdr>(0);
+}
+
+const ElfCoreDump::Phdr* ElfCoreDump::GetProgramHeader(unsigned index) const {
+ const Ehdr* header = GetHeader();
+ if (header) {
+ return reinterpret_cast<const Phdr*>(content_.GetArrayElement(
+ header->e_phoff, header->e_phentsize, index));
+ }
+ return NULL;
+}
+
+const ElfCoreDump::Phdr* ElfCoreDump::GetFirstProgramHeaderOfType(
+ Word type) const {
+ for (unsigned i = 0, n = GetProgramHeaderCount(); i < n; ++i) {
+ const Phdr* program = GetProgramHeader(i);
+ if (program->p_type == type) {
+ return program;
+ }
+ }
+ return NULL;
+}
+
+unsigned ElfCoreDump::GetProgramHeaderCount() const {
+ const Ehdr* header = GetHeader();
+ return header ? header->e_phnum : 0;
+}
+
+bool ElfCoreDump::CopyData(void* buffer, Addr virtual_address, size_t length) {
+ for (unsigned i = 0, n = GetProgramHeaderCount(); i < n; ++i) {
+ const Phdr* program = GetProgramHeader(i);
+ if (program->p_type != PT_LOAD)
+ continue;
+
+ size_t offset_in_segment = virtual_address - program->p_vaddr;
+ if (virtual_address >= program->p_vaddr &&
+ offset_in_segment < program->p_filesz) {
+ const void* data =
+ content_.GetData(program->p_offset + offset_in_segment, length);
+ if (data) {
+ memcpy(buffer, data, length);
+ return true;
+ }
+ }
+ }
+ return false;
+}
+
+ElfCoreDump::Note ElfCoreDump::GetFirstNote() const {
+ MemoryRange note_content;
+ const Phdr* program_header = GetFirstProgramHeaderOfType(PT_NOTE);
+ if (program_header) {
+ note_content = content_.Subrange(program_header->p_offset,
+ program_header->p_filesz);
+ }
+ return Note(note_content);
+}
+
+} // namespace google_breakpad
diff --git a/src/common/linux/elf_core_dump.h b/src/common/linux/elf_core_dump.h
new file mode 100644
index 00000000..dd846f09
--- /dev/null
+++ b/src/common/linux/elf_core_dump.h
@@ -0,0 +1,153 @@
+// Copyright (c) 2011, Google Inc.
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// elf_core_dump.h: Define the google_breakpad::ElfCoreDump class, which
+// encapsulates an ELF core dump file mapped into memory.
+
+#ifndef COMMON_LINUX_ELF_CORE_DUMP_H_
+#define COMMON_LINUX_ELF_CORE_DUMP_H_
+
+#include <elf.h>
+#if !defined(__ANDROID__)
+#include <link.h>
+#endif
+#include <stddef.h>
+
+#include "common/memory_range.h"
+#if defined(__ANDROID__)
+#include "common/linux/android_link.h"
+#endif
+
+namespace google_breakpad {
+
+// A class encapsulating an ELF core dump file mapped into memory, which
+// provides methods for accessing program headers and the note section.
+class ElfCoreDump {
+ public:
+ // ELF types based on the value of __WORDSIZE.
+ typedef ElfW(Ehdr) Ehdr;
+ typedef ElfW(Nhdr) Nhdr;
+ typedef ElfW(Phdr) Phdr;
+ typedef ElfW(Word) Word;
+ typedef ElfW(Addr) Addr;
+#if __WORDSIZE == 32
+ static const int kClass = ELFCLASS32;
+#elif __WORDSIZE == 64
+ static const int kClass = ELFCLASS64;
+#else
+#error "Unsupported __WORDSIZE for ElfCoreDump."
+#endif
+
+ // A class encapsulating the note content in a core dump, which provides
+ // methods for accessing the name and description of a note.
+ class Note {
+ public:
+ Note();
+
+ // Constructor that takes the note content from |content|.
+ explicit Note(const MemoryRange& content);
+
+ // Returns true if this note is valid, i,e. a note header is found in
+ // |content_|, or false otherwise.
+ bool IsValid() const;
+
+ // Returns the note header, or NULL if no note header is found in
+ // |content_|.
+ const Nhdr* GetHeader() const;
+
+ // Returns the note type, or 0 if no note header is found in |content_|.
+ Word GetType() const;
+
+ // Returns a memory range covering the note name, or an empty range
+ // if no valid note name is found in |content_|.
+ MemoryRange GetName() const;
+
+ // Returns a memory range covering the note description, or an empty
+ // range if no valid note description is found in |content_|.
+ MemoryRange GetDescription() const;
+
+ // Returns the note following this note, or an empty note if no valid
+ // note is found after this note.
+ Note GetNextNote() const;
+
+ private:
+ // Returns the size in bytes round up to the word alignment, specified
+ // for the note section, of a given size in bytes.
+ static size_t AlignedSize(size_t size);
+
+ // Note content.
+ MemoryRange content_;
+ };
+
+ ElfCoreDump();
+
+ // Constructor that takes the core dump content from |content|.
+ explicit ElfCoreDump(const MemoryRange& content);
+
+ // Sets the core dump content to |content|.
+ void SetContent(const MemoryRange& content);
+
+ // Returns true if a valid ELF header in the core dump, or false otherwise.
+ bool IsValid() const;
+
+ // Returns the ELF header in the core dump, or NULL if no ELF header
+ // is found in |content_|.
+ const Ehdr* GetHeader() const;
+
+ // Returns the |index|-th program header in the core dump, or NULL if no
+ // ELF header is found in |content_| or |index| is out of bounds.
+ const Phdr* GetProgramHeader(unsigned index) const;
+
+ // Returns the first program header of |type| in the core dump, or NULL if
+ // no ELF header is found in |content_| or no program header of |type| is
+ // found.
+ const Phdr* GetFirstProgramHeaderOfType(Word type) const;
+
+ // Returns the number of program headers in the core dump, or 0 if no
+ // ELF header is found in |content_|.
+ unsigned GetProgramHeaderCount() const;
+
+ // Copies |length| bytes of data starting at |virtual_address| in the core
+ // dump to |buffer|. |buffer| should be a valid pointer to a buffer of at
+ // least |length| bytes. Returns true if the data to be copied is found in
+ // the core dump, or false otherwise.
+ bool CopyData(void* buffer, Addr virtual_address, size_t length);
+
+ // Returns the first note found in the note section of the core dump, or
+ // an empty note if no note is found.
+ Note GetFirstNote() const;
+
+ private:
+ // Core dump content.
+ MemoryRange content_;
+};
+
+} // namespace google_breakpad
+
+#endif // COMMON_LINUX_ELF_CORE_DUMP_H_
diff --git a/src/common/linux/elf_core_dump_unittest.cc b/src/common/linux/elf_core_dump_unittest.cc
new file mode 100644
index 00000000..324e57b6
--- /dev/null
+++ b/src/common/linux/elf_core_dump_unittest.cc
@@ -0,0 +1,238 @@
+// Copyright (c) 2011, Google Inc.
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// elf_core_dump_unittest.cc: Unit tests for google_breakpad::ElfCoreDump.
+
+#include <sys/procfs.h>
+
+#include <set>
+#include <string>
+
+#include "breakpad_googletest_includes.h"
+#include "common/linux/elf_core_dump.h"
+#include "common/linux/memory_mapped_file.h"
+#include "common/tests/file_utils.h"
+#include "common/linux/tests/crash_generator.h"
+
+using google_breakpad::AutoTempDir;
+using google_breakpad::CrashGenerator;
+using google_breakpad::ElfCoreDump;
+using google_breakpad::MemoryMappedFile;
+using google_breakpad::MemoryRange;
+using google_breakpad::WriteFile;
+using std::set;
+using std::string;
+
+TEST(ElfCoreDumpTest, DefaultConstructor) {
+ ElfCoreDump core;
+ EXPECT_FALSE(core.IsValid());
+ EXPECT_EQ(NULL, core.GetHeader());
+ EXPECT_EQ(0, core.GetProgramHeaderCount());
+ EXPECT_EQ(NULL, core.GetProgramHeader(0));
+ EXPECT_EQ(NULL, core.GetFirstProgramHeaderOfType(PT_LOAD));
+ EXPECT_FALSE(core.GetFirstNote().IsValid());
+}
+
+TEST(ElfCoreDumpTest, TestElfHeader) {
+ ElfCoreDump::Ehdr header;
+ memset(&header, 0, sizeof(header));
+
+ AutoTempDir temp_dir;
+ string core_path = temp_dir.path() + "/core";
+ const char* core_file = core_path.c_str();
+ MemoryMappedFile mapped_core_file;
+ ElfCoreDump core;
+
+ ASSERT_TRUE(WriteFile(core_file, &header, sizeof(header) - 1));
+ ASSERT_TRUE(mapped_core_file.Map(core_file));
+ core.SetContent(mapped_core_file.content());
+ EXPECT_FALSE(core.IsValid());
+ EXPECT_EQ(NULL, core.GetHeader());
+ EXPECT_EQ(0, core.GetProgramHeaderCount());
+ EXPECT_EQ(NULL, core.GetProgramHeader(0));
+ EXPECT_EQ(NULL, core.GetFirstProgramHeaderOfType(PT_LOAD));
+ EXPECT_FALSE(core.GetFirstNote().IsValid());
+
+ ASSERT_TRUE(WriteFile(core_file, &header, sizeof(header)));
+ ASSERT_TRUE(mapped_core_file.Map(core_file));
+ core.SetContent(mapped_core_file.content());
+ EXPECT_FALSE(core.IsValid());
+
+ header.e_ident[0] = ELFMAG0;
+ ASSERT_TRUE(WriteFile(core_file, &header, sizeof(header)));
+ ASSERT_TRUE(mapped_core_file.Map(core_file));
+ core.SetContent(mapped_core_file.content());
+ EXPECT_FALSE(core.IsValid());
+
+ header.e_ident[1] = ELFMAG1;
+ ASSERT_TRUE(WriteFile(core_file, &header, sizeof(header)));
+ ASSERT_TRUE(mapped_core_file.Map(core_file));
+ core.SetContent(mapped_core_file.content());
+ EXPECT_FALSE(core.IsValid());
+
+ header.e_ident[2] = ELFMAG2;
+ ASSERT_TRUE(WriteFile(core_file, &header, sizeof(header)));
+ ASSERT_TRUE(mapped_core_file.Map(core_file));
+ core.SetContent(mapped_core_file.content());
+ EXPECT_FALSE(core.IsValid());
+
+ header.e_ident[3] = ELFMAG3;
+ ASSERT_TRUE(WriteFile(core_file, &header, sizeof(header)));
+ ASSERT_TRUE(mapped_core_file.Map(core_file));
+ core.SetContent(mapped_core_file.content());
+ EXPECT_FALSE(core.IsValid());
+
+ header.e_ident[4] = ElfCoreDump::kClass;
+ ASSERT_TRUE(WriteFile(core_file, &header, sizeof(header)));
+ ASSERT_TRUE(mapped_core_file.Map(core_file));
+ core.SetContent(mapped_core_file.content());
+ EXPECT_FALSE(core.IsValid());
+
+ header.e_version = EV_CURRENT;
+ ASSERT_TRUE(WriteFile(core_file, &header, sizeof(header)));
+ ASSERT_TRUE(mapped_core_file.Map(core_file));
+ core.SetContent(mapped_core_file.content());
+ EXPECT_FALSE(core.IsValid());
+
+ header.e_type = ET_CORE;
+ ASSERT_TRUE(WriteFile(core_file, &header, sizeof(header)));
+ ASSERT_TRUE(mapped_core_file.Map(core_file));
+ core.SetContent(mapped_core_file.content());
+ EXPECT_TRUE(core.IsValid());
+}
+
+TEST(ElfCoreDumpTest, ValidCoreFile) {
+ CrashGenerator crash_generator;
+ if (!crash_generator.HasDefaultCorePattern()) {
+ fprintf(stderr, "ElfCoreDumpTest.ValidCoreFile test is skipped");
+ return;
+ }
+
+ const unsigned kNumOfThreads = 3;
+ const unsigned kCrashThread = 1;
+ const int kCrashSignal = SIGABRT;
+ ASSERT_TRUE(crash_generator.CreateChildCrash(kNumOfThreads, kCrashThread,
+ kCrashSignal));
+ pid_t expected_crash_thread_id = crash_generator.GetThreadId(kCrashThread);
+ set<pid_t> expected_thread_ids;
+ for (unsigned i = 0; i < kNumOfThreads; ++i) {
+ expected_thread_ids.insert(crash_generator.GetThreadId(i));
+ }
+
+ MemoryMappedFile mapped_core_file;
+ ASSERT_TRUE(mapped_core_file.Map(crash_generator.GetCoreFilePath().c_str()));
+
+ ElfCoreDump core;
+ core.SetContent(mapped_core_file.content());
+ EXPECT_TRUE(core.IsValid());
+
+ // Based on write_note_info() in linux/kernel/fs/binfmt_elf.c, notes are
+ // ordered as follows (NT_PRXFPREG and NT_386_TLS are i386 specific):
+ // Thread Name Type
+ // -------------------------------------------------------------------
+ // 1st thread CORE NT_PRSTATUS
+ // process-wide CORE NT_PRPSINFO
+ // process-wide CORE NT_AUXV
+ // 1st thread CORE NT_FPREGSET
+ // 1st thread LINUX NT_PRXFPREG
+ // 1st thread LINUX NT_386_TLS
+ //
+ // 2nd thread CORE NT_PRSTATUS
+ // 2nd thread CORE NT_FPREGSET
+ // 2nd thread LINUX NT_PRXFPREG
+ // 2nd thread LINUX NT_386_TLS
+ //
+ // 3rd thread CORE NT_PRSTATUS
+ // 3rd thread CORE NT_FPREGSET
+ // 3rd thread LINUX NT_PRXFPREG
+ // 3rd thread LINUX NT_386_TLS
+
+ size_t num_nt_prpsinfo = 0;
+ size_t num_nt_prstatus = 0;
+ size_t num_nt_fpregset = 0;
+ size_t num_nt_prxfpreg = 0;
+ set<pid_t> actual_thread_ids;
+ ElfCoreDump::Note note = core.GetFirstNote();
+ while (note.IsValid()) {
+ MemoryRange name = note.GetName();
+ MemoryRange description = note.GetDescription();
+ EXPECT_FALSE(name.IsEmpty());
+ EXPECT_FALSE(description.IsEmpty());
+
+ switch (note.GetType()) {
+ case NT_PRPSINFO: {
+ EXPECT_TRUE(description.data() != NULL);
+ EXPECT_EQ(sizeof(elf_prpsinfo), description.length());
+ ++num_nt_prpsinfo;
+ break;
+ }
+ case NT_PRSTATUS: {
+ EXPECT_TRUE(description.data() != NULL);
+ EXPECT_EQ(sizeof(elf_prstatus), description.length());
+ const elf_prstatus* status = description.GetData<elf_prstatus>(0);
+ actual_thread_ids.insert(status->pr_pid);
+ if (num_nt_prstatus == 0) {
+ EXPECT_EQ(expected_crash_thread_id, status->pr_pid);
+ EXPECT_EQ(kCrashSignal, status->pr_info.si_signo);
+ }
+ ++num_nt_prstatus;
+ break;
+ }
+#if defined(__i386) || defined(__x86_64)
+ case NT_FPREGSET: {
+ EXPECT_TRUE(description.data() != NULL);
+ EXPECT_EQ(sizeof(user_fpregs_struct), description.length());
+ ++num_nt_fpregset;
+ break;
+ }
+#endif
+#if defined(__i386)
+ case NT_PRXFPREG: {
+ EXPECT_TRUE(description.data() != NULL);
+ EXPECT_EQ(sizeof(user_fpxregs_struct), description.length());
+ ++num_nt_prxfpreg;
+ break;
+ }
+#endif
+ default:
+ break;
+ }
+ note = note.GetNextNote();
+ }
+
+ EXPECT_TRUE(expected_thread_ids == actual_thread_ids);
+ EXPECT_EQ(1, num_nt_prpsinfo);
+ EXPECT_EQ(kNumOfThreads, num_nt_prstatus);
+#if defined(__i386) || defined(__x86_64)
+ EXPECT_EQ(kNumOfThreads, num_nt_fpregset);
+#endif
+#if defined(__i386)
+ EXPECT_EQ(kNumOfThreads, num_nt_prxfpreg);
+#endif
+}
diff --git a/src/common/linux/memory_mapped_file_unittest.cc b/src/common/linux/memory_mapped_file_unittest.cc
index 8a3129fb..cf89bca9 100644
--- a/src/common/linux/memory_mapped_file_unittest.cc
+++ b/src/common/linux/memory_mapped_file_unittest.cc
@@ -40,32 +40,15 @@
#include "common/linux/eintr_wrapper.h"
#include "common/linux/memory_mapped_file.h"
#include "common/tests/auto_tempdir.h"
+#include "common/tests/file_utils.h"
using google_breakpad::AutoTempDir;
using google_breakpad::MemoryMappedFile;
+using google_breakpad::WriteFile;
using std::string;
namespace {
-bool WriteFile(const string& path, const void* buffer, size_t buffer_size) {
- int fd =
- HANDLE_EINTR(open(path.c_str(), O_CREAT | O_TRUNC | O_WRONLY, S_IRWXU));
- if (fd == -1) {
- perror("open");
- return false;
- }
-
- bool ok = true;
- if (buffer && buffer_size > 0) {
- if (HANDLE_EINTR(write(fd, buffer, buffer_size) != buffer_size)) {
- perror("write");
- ok = false;
- }
- }
- close(fd);
- return ok;
-}
-
class MemoryMappedFileTest : public testing::Test {
protected:
void ExpectNoMappedData(const MemoryMappedFile& mapped_file) {
@@ -102,7 +85,7 @@ TEST_F(MemoryMappedFileTest, MapNonexistentFile) {
TEST_F(MemoryMappedFileTest, MapEmptyFile) {
AutoTempDir temp_dir;
string test_file = temp_dir.path() + "/empty_file";
- ASSERT_TRUE(WriteFile(test_file, NULL, 0));
+ ASSERT_TRUE(WriteFile(test_file.c_str(), NULL, 0));
{
MemoryMappedFile mapped_file(test_file.c_str());
@@ -124,7 +107,7 @@ TEST_F(MemoryMappedFileTest, MapNonEmptyFile) {
AutoTempDir temp_dir;
string test_file = temp_dir.path() + "/test_file";
- ASSERT_TRUE(WriteFile(test_file, data, data_size));
+ ASSERT_TRUE(WriteFile(test_file.c_str(), data, data_size));
{
MemoryMappedFile mapped_file(test_file.c_str());
@@ -159,8 +142,8 @@ TEST_F(MemoryMappedFileTest, RemapAfterMap) {
AutoTempDir temp_dir;
string test_file1 = temp_dir.path() + "/test_file1";
string test_file2 = temp_dir.path() + "/test_file2";
- ASSERT_TRUE(WriteFile(test_file1, data1, data1_size));
- ASSERT_TRUE(WriteFile(test_file2, data2, data2_size));
+ ASSERT_TRUE(WriteFile(test_file1.c_str(), data1, data1_size));
+ ASSERT_TRUE(WriteFile(test_file2.c_str(), data2, data2_size));
{
MemoryMappedFile mapped_file(test_file1.c_str());
diff --git a/src/common/linux/tests/crash_generator.cc b/src/common/linux/tests/crash_generator.cc
new file mode 100644
index 00000000..3ef40dbd
--- /dev/null
+++ b/src/common/linux/tests/crash_generator.cc
@@ -0,0 +1,206 @@
+// Copyright (c) 2011, Google Inc.
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// crash_generator.cc: Implement google_breakpad::CrashGenerator.
+// See crash_generator.h for details.
+
+#include "common/linux/tests/crash_generator.h"
+
+#include <pthread.h>
+#include <signal.h>
+#include <sys/mman.h>
+#include <sys/resource.h>
+#include <sys/syscall.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#include <string>
+
+#include "common/linux/eintr_wrapper.h"
+#include "common/tests/auto_tempdir.h"
+#include "common/tests/file_utils.h"
+
+namespace {
+
+struct ThreadData {
+ pthread_t thread;
+ pthread_barrier_t* barrier;
+ pid_t* thread_id_ptr;
+};
+
+// Core file size limit set to 1 MB, which is big enough for test purposes.
+const rlim_t kCoreSizeLimit = 1024 * 1024;
+
+void *thread_function(void *data) {
+ ThreadData* thread_data = reinterpret_cast<ThreadData*>(data);
+ volatile pid_t thread_id = syscall(__NR_gettid);
+ *(thread_data->thread_id_ptr) = thread_id;
+ int result = pthread_barrier_wait(thread_data->barrier);
+ if (result != 0 && result != PTHREAD_BARRIER_SERIAL_THREAD) {
+ exit(1);
+ }
+ while (true) {
+ pthread_yield();
+ }
+}
+
+} // namespace
+
+namespace google_breakpad {
+
+CrashGenerator::CrashGenerator()
+ : shared_memory_(NULL),
+ shared_memory_size_(0) {
+}
+
+CrashGenerator::~CrashGenerator() {
+ UnmapSharedMemory();
+}
+
+bool CrashGenerator::HasDefaultCorePattern() const {
+ char buffer[8];
+ ssize_t buffer_size = sizeof(buffer);
+ return ReadFile("/proc/sys/kernel/core_pattern", buffer, &buffer_size) &&
+ buffer_size == 5 && memcmp(buffer, "core", 4) == 0;
+}
+
+std::string CrashGenerator::GetCoreFilePath() const {
+ return temp_dir_.path() + "/core";
+}
+
+pid_t CrashGenerator::GetThreadId(unsigned index) const {
+ return reinterpret_cast<pid_t*>(shared_memory_)[index];
+}
+
+pid_t* CrashGenerator::GetThreadIdPointer(unsigned index) {
+ return reinterpret_cast<pid_t*>(shared_memory_) + index;
+}
+
+bool CrashGenerator::MapSharedMemory(size_t memory_size) {
+ if (!UnmapSharedMemory())
+ return false;
+
+ void* mapped_memory = mmap(0, memory_size, PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+ if (mapped_memory == MAP_FAILED)
+ return false;
+
+ memset(mapped_memory, 0, memory_size);
+ shared_memory_ = mapped_memory;
+ shared_memory_size_ = memory_size;
+ return true;
+}
+
+bool CrashGenerator::UnmapSharedMemory() {
+ if (!shared_memory_)
+ return true;
+
+ if (munmap(shared_memory_, shared_memory_size_) == 0) {
+ shared_memory_ = NULL;
+ shared_memory_size_ = 0;
+ return true;
+ }
+ return false;
+}
+
+bool CrashGenerator::SetCoreFileSizeLimit(rlim_t limit) const {
+ struct rlimit limits = { limit, limit };
+ return setrlimit(RLIMIT_CORE, &limits) == 0;
+}
+
+bool CrashGenerator::CreateChildCrash(
+ unsigned num_threads, unsigned crash_thread, int crash_signal) {
+ if (num_threads == 0 || crash_thread >= num_threads)
+ return false;
+
+ if (!MapSharedMemory(num_threads * sizeof(pid_t)))
+ return false;
+
+ pid_t pid = fork();
+ if (pid == 0) {
+ if (chdir(temp_dir_.path().c_str()) == 0 &&
+ SetCoreFileSizeLimit(kCoreSizeLimit)) {
+ CreateThreadsInChildProcess(num_threads);
+ kill(*GetThreadIdPointer(crash_thread), crash_signal);
+ }
+ exit(1);
+ }
+
+ int status;
+ if (HANDLE_EINTR(waitpid(pid, &status, 0)) == -1 ||
+ !WIFSIGNALED(status) || WTERMSIG(status) != crash_signal)
+ return false;
+
+ return true;
+}
+
+void CrashGenerator::CreateThreadsInChildProcess(unsigned num_threads) {
+ *GetThreadIdPointer(0) = getpid();
+
+ if (num_threads <= 1)
+ return;
+
+ // This method does not clean up any pthread resource, as the process
+ // is expected to be killed anyway.
+ ThreadData* thread_data = new ThreadData[num_threads];
+
+ // Create detached threads so that we do not worry about pthread_join()
+ // later being called or not.
+ pthread_attr_t thread_attributes;
+ if (pthread_attr_init(&thread_attributes) != 0 ||
+ pthread_attr_setdetachstate(&thread_attributes,
+ PTHREAD_CREATE_DETACHED) != 0) {
+ exit(1);
+ }
+
+ pthread_barrier_t thread_barrier;
+ if (pthread_barrier_init(&thread_barrier, NULL, num_threads) != 0) {
+ exit(1);
+ }
+
+ for (unsigned i = 1; i < num_threads; ++i) {
+ thread_data[i].barrier = &thread_barrier;
+ thread_data[i].thread_id_ptr = GetThreadIdPointer(i);
+ if (pthread_create(&thread_data[i].thread, &thread_attributes,
+ thread_function, &thread_data[i]) != 0) {
+ exit(1);
+ }
+ }
+
+ int result = pthread_barrier_wait(&thread_barrier);
+ if (result != 0 && result != PTHREAD_BARRIER_SERIAL_THREAD) {
+ exit(1);
+ }
+
+ pthread_barrier_destroy(&thread_barrier);
+ pthread_attr_destroy(&thread_attributes);
+ delete[] thread_data;
+}
+
+} // namespace google_breakpad
diff --git a/src/common/linux/tests/crash_generator.h b/src/common/linux/tests/crash_generator.h
new file mode 100644
index 00000000..c88a8920
--- /dev/null
+++ b/src/common/linux/tests/crash_generator.h
@@ -0,0 +1,108 @@
+// Copyright (c) 2011, Google Inc.
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// crash_generator.h: Define the google_breakpad::CrashGenerator class,
+// which is used to generate a crash (and a core dump file) for testing.
+
+#ifndef COMMON_LINUX_TESTS_CRASH_GENERATOR_H_
+#define COMMON_LINUX_TESTS_CRASH_GENERATOR_H_
+
+#include <sys/resource.h>
+
+#include <string>
+
+#include "common/tests/auto_tempdir.h"
+
+namespace google_breakpad {
+
+// A utility class for generating a crash (and a core dump file) for
+// testing. It creates a child process with the specified number of
+// threads, which is then termainated by the specified signal. A core
+// dump file is expected to be created upon the termination of the child
+// process, which can then be used for testing code that processes core
+// dump files.
+class CrashGenerator {
+ public:
+ CrashGenerator();
+
+ ~CrashGenerator();
+
+ // Returns true if a core dump file named 'core' will be generated in
+ // the current directory for a test that produces a crash by checking
+ // if /proc/sys/kernel/core_pattern has the default value 'core'.
+ bool HasDefaultCorePattern() const;
+
+ // Sets the maximum size of core dump file (both the soft and hard limit)
+ // to |limit| bytes. Returns true on success.
+ bool SetCoreFileSizeLimit(rlim_t limit) const;
+
+ // Returns the expected path of the core dump file.
+ std::string GetCoreFilePath() const;
+
+ // Creates a crash (and a core dump file) by creating a child process with
+ // |num_threads| threads, and the terminating the child process by sending
+ // a signal with number |crash_signal| to the |crash_thread|-th thread.
+ // Returns true on success.
+ bool CreateChildCrash(unsigned num_threads, unsigned crash_thread,
+ int crash_signal);
+
+ // Creates |num_threads| threads in the child process.
+ void CreateThreadsInChildProcess(unsigned num_threads);
+
+ // Returns the thread ID of the |index|-th thread in the child process.
+ // This method does not validate |index|.
+ pid_t GetThreadId(unsigned index) const;
+
+ private:
+ // Creates a shared memory of |memory_size| bytes for communicating thread
+ // IDs between the parent and child process. Returns true on success.
+ bool MapSharedMemory(size_t memory_size);
+
+ // Releases any shared memory created by MapSharedMemory(). Returns true on
+ // success.
+ bool UnmapSharedMemory();
+
+ // Returns the pointer to the thread ID of the |index|-th thread in the child
+ // process. This method does not validate |index|.
+ pid_t* GetThreadIdPointer(unsigned index);
+
+ // Temporary directory in which a core file is generated.
+ AutoTempDir temp_dir_;
+
+ // Shared memory for communicating thread IDs between the parent and
+ // child process.
+ void* shared_memory_;
+
+ // Number of bytes mapped for |shared_memory_|.
+ size_t shared_memory_size_;
+};
+
+} // namespace google_breakpad
+
+#endif // COMMON_LINUX_TESTS_CRASH_GENERATOR_H_
diff --git a/src/common/tests/auto_tempdir.h b/src/common/tests/auto_tempdir.h
index e78d7391..ccfd81bc 100644
--- a/src/common/tests/auto_tempdir.h
+++ b/src/common/tests/auto_tempdir.h
@@ -59,9 +59,9 @@ class AutoTempDir {
DeleteRecursively(path_);
}
- const std::string& path() {
- return path_;
- }
+ const std::string& path() const {
+ return path_;
+ }
private:
void DeleteRecursively(const std::string& path) {
diff --git a/src/common/tests/file_utils.cc b/src/common/tests/file_utils.cc
new file mode 100644
index 00000000..0d9b04ad
--- /dev/null
+++ b/src/common/tests/file_utils.cc
@@ -0,0 +1,97 @@
+// Copyright (c) 2011, Google Inc.
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// file_utils.cc: Implement utility functions for file manipulation.
+// See file_utils.h for details.
+
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "common/linux/eintr_wrapper.h"
+#include "common/tests/file_utils.h"
+
+namespace google_breakpad {
+
+bool ReadFile(const char* path, void* buffer, ssize_t* buffer_size) {
+ int fd = HANDLE_EINTR(open(path, O_RDONLY));
+ if (fd == -1) {
+ perror("open");
+ return false;
+ }
+
+ bool ok = true;
+ if (buffer && buffer_size && *buffer_size > 0) {
+ memset(buffer, 0, sizeof(*buffer_size));
+ *buffer_size = HANDLE_EINTR(read(fd, buffer, *buffer_size));
+ if (*buffer_size == -1) {
+ perror("read");
+ ok = false;
+ }
+ }
+ if (HANDLE_EINTR(close(fd)) == -1) {
+ perror("close");
+ ok = false;
+ }
+ return ok;
+}
+
+bool WriteFile(const char* path, const void* buffer, size_t buffer_size) {
+ int fd = HANDLE_EINTR(open(path, O_CREAT | O_TRUNC | O_WRONLY, S_IRWXU));
+ if (fd == -1) {
+ perror("open");
+ return false;
+ }
+
+ bool ok = true;
+ if (buffer) {
+ size_t bytes_written_total = 0;
+ ssize_t bytes_written_partial = 0;
+ const char* data = reinterpret_cast<const char*>(buffer);
+ while (bytes_written_total < buffer_size) {
+ bytes_written_partial =
+ HANDLE_EINTR(write(fd, data + bytes_written_total,
+ buffer_size - bytes_written_total));
+ if (bytes_written_partial < 0) {
+ perror("write");
+ ok = false;
+ break;
+ }
+ bytes_written_total += bytes_written_partial;
+ }
+ }
+ if (HANDLE_EINTR(close(fd)) == -1) {
+ perror("close");
+ ok = false;
+ }
+ return ok;
+}
+
+} // namespace google_breakpad
diff --git a/src/common/tests/file_utils.h b/src/common/tests/file_utils.h
new file mode 100644
index 00000000..52e8b009
--- /dev/null
+++ b/src/common/tests/file_utils.h
@@ -0,0 +1,49 @@
+// Copyright (c) 2011, Google Inc.
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// file_utils.h: Define utility functions for file manipulation, which
+// are used for testing.
+
+#ifndef COMMON_TESTS_FILE_UTILS_H_
+#define COMMON_TESTS_FILE_UTILS_H_
+
+namespace google_breakpad {
+
+// Reads the content of a file at |path| into |buffer|. |buffer_size| specifies
+// the size of |buffer| in bytes and returns the number of bytes read from the
+// file on success. Returns true on success.
+bool ReadFile(const char* path, void* buffer, ssize_t* buffer_size);
+
+// Writes |buffer_size| bytes of the content in |buffer| to a file at |path|.
+// Returns true on success.
+bool WriteFile(const char* path, const void* buffer, size_t buffer_size);
+
+} // namespace google_breakpad
+
+#endif // COMMON_TESTS_FILE_UTILS_H_