Pull (most) platform-specific MemorySnapshots out

Pulls the concrete non-test implementations of MemorySnapshot out into a
template. They were effectively identical on Mac and Linux/Android, and
I was going to have to add another identical one for Fuchsia.
Unfortunately it needs to be a template because of the snapshot merging
template it calls that needs the platform-specific ProcessReader (so it
can't just pass in a base ProcessMemory in initialization instead).

This is used on Mac, Linux, Android, and Fuchsia, but there is still a
Windows implementation (different because its ProcessReader is a bit
different) and a test implementation.

Bug: crashpad:196
Change-Id: I4b5575fee0749e96b08e756be1f8380a2c994d7c
Reviewed-on: https://chromium-review.googlesource.com/929308
Reviewed-by: Mark Mentovai <mark@chromium.org>
Commit-Queue: Scott Graham <scottmg@chromium.org>
This commit is contained in:
Scott Graham 2018-02-21 16:07:17 -08:00 committed by Commit Bot
parent 4d96e4e504
commit 2290a826af
8 changed files with 56 additions and 253 deletions

View File

@ -34,6 +34,7 @@ static_library("snapshot") {
"handle_snapshot.h",
"memory_snapshot.cc",
"memory_snapshot.h",
"memory_snapshot_generic.h",
"minidump/minidump_annotation_reader.cc",
"minidump/minidump_annotation_reader.h",
"minidump/minidump_simple_string_dictionary_reader.cc",
@ -76,8 +77,6 @@ static_library("snapshot") {
"mac/mach_o_image_segment_reader.h",
"mac/mach_o_image_symbol_table_reader.cc",
"mac/mach_o_image_symbol_table_reader.h",
"mac/memory_snapshot_mac.cc",
"mac/memory_snapshot_mac.h",
"mac/module_snapshot_mac.cc",
"mac/module_snapshot_mac.h",
"mac/process_reader.cc",
@ -112,8 +111,6 @@ static_library("snapshot") {
"linux/debug_rendezvous.h",
"linux/exception_snapshot_linux.cc",
"linux/exception_snapshot_linux.h",
"linux/memory_snapshot_linux.cc",
"linux/memory_snapshot_linux.h",
"linux/process_reader.cc",
"linux/process_reader.h",
"linux/process_snapshot_linux.cc",

View File

@ -1,73 +0,0 @@
// Copyright 2017 The Crashpad Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "snapshot/linux/memory_snapshot_linux.h"
#include <memory>
namespace crashpad {
namespace internal {
MemorySnapshotLinux::MemorySnapshotLinux()
: MemorySnapshot(),
process_reader_(nullptr),
address_(0),
size_(0),
initialized_() {
}
MemorySnapshotLinux::~MemorySnapshotLinux() {
}
void MemorySnapshotLinux::Initialize(ProcessReader* process_reader,
LinuxVMAddress address,
size_t size) {
INITIALIZATION_STATE_SET_INITIALIZING(initialized_);
process_reader_ = process_reader;
address_ = address;
size_ = size;
INITIALIZATION_STATE_SET_VALID(initialized_);
}
uint64_t MemorySnapshotLinux::Address() const {
INITIALIZATION_STATE_DCHECK_VALID(initialized_);
return address_;
}
size_t MemorySnapshotLinux::Size() const {
INITIALIZATION_STATE_DCHECK_VALID(initialized_);
return size_;
}
bool MemorySnapshotLinux::Read(Delegate* delegate) const {
INITIALIZATION_STATE_DCHECK_VALID(initialized_);
if (size_ == 0) {
return delegate->MemorySnapshotDelegateRead(nullptr, size_);
}
std::unique_ptr<uint8_t[]> buffer(new uint8_t[size_]);
if (!process_reader_->Memory()->Read(address_, size_, buffer.get())) {
return false;
}
return delegate->MemorySnapshotDelegateRead(buffer.get(), size_);
}
const MemorySnapshot* MemorySnapshotLinux::MergeWithOtherSnapshot(
const MemorySnapshot* other) const {
return MergeWithOtherSnapshotImpl(this, other);
}
} // namespace internal
} // namespace crashpad

View File

@ -1,76 +0,0 @@
// Copyright 2017 The Crashpad Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef CRASHPAD_SNAPSHOT_LINUX_MEMORY_SNAPSHOT_LINUX_H_
#define CRASHPAD_SNAPSHOT_LINUX_MEMORY_SNAPSHOT_LINUX_H_
#include <stddef.h>
#include <stdint.h>
#include "base/macros.h"
#include "snapshot/linux/process_reader.h"
#include "snapshot/memory_snapshot.h"
#include "util/linux/address_types.h"
#include "util/misc/initialization_state_dcheck.h"
namespace crashpad {
namespace internal {
//! \brief A MemorySnapshot of a memory region in a process on the running
//! system, when the system runs Linux.
class MemorySnapshotLinux final : public MemorySnapshot {
public:
MemorySnapshotLinux();
~MemorySnapshotLinux() override;
//! \brief Initializes the object.
//!
//! Memory is read lazily. No attempt is made to read the memory snapshot data
//! until Read() is called, and the memory snapshot data is discared when
//! Read() returns.
//!
//! \param[in] process_reader A reader for the process being snapshotted.
//! \param[in] address The base address of the memory region to snapshot, in
//! the snapshot process address space.
//! \param[in] size The size of the memory region to snapshot.
void Initialize(ProcessReader* process_reader,
LinuxVMAddress address,
size_t size);
// MemorySnapshot:
uint64_t Address() const override;
size_t Size() const override;
bool Read(Delegate* delegate) const override;
const MemorySnapshot* MergeWithOtherSnapshot(
const MemorySnapshot* other) const override;
private:
template <class T>
friend const MemorySnapshot* MergeWithOtherSnapshotImpl(
const T* self,
const MemorySnapshot* other);
ProcessReader* process_reader_; // weak
uint64_t address_;
size_t size_;
InitializationStateDcheck initialized_;
DISALLOW_COPY_AND_ASSIGN(MemorySnapshotLinux);
};
} // namespace internal
} // namespace crashpad
#endif // CRASHPAD_SNAPSHOT_LINUX_MEMORY_SNAPSHOT_LINUX_H_

View File

@ -20,9 +20,9 @@
#include "base/macros.h"
#include "build/build_config.h"
#include "snapshot/cpu_context.h"
#include "snapshot/linux/memory_snapshot_linux.h"
#include "snapshot/linux/process_reader.h"
#include "snapshot/memory_snapshot.h"
#include "snapshot/memory_snapshot_generic.h"
#include "snapshot/thread_snapshot.h"
#include "util/misc/initialization_state_dcheck.h"
@ -70,7 +70,7 @@ class ThreadSnapshotLinux final : public ThreadSnapshot {
#endif // ARCH_CPU_X86_FAMILY
} context_union_;
CPUContext context_;
MemorySnapshotLinux stack_;
MemorySnapshotGeneric<ProcessReader> stack_;
LinuxVMAddress thread_specific_data_address_;
pid_t thread_id_;
int priority_;

View File

@ -1,75 +0,0 @@
// Copyright 2014 The Crashpad Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "snapshot/mac/memory_snapshot_mac.h"
#include <memory>
#include "util/mach/task_memory.h"
namespace crashpad {
namespace internal {
MemorySnapshotMac::MemorySnapshotMac()
: MemorySnapshot(),
process_reader_(nullptr),
address_(0),
size_(0),
initialized_() {
}
MemorySnapshotMac::~MemorySnapshotMac() {
}
void MemorySnapshotMac::Initialize(ProcessReader* process_reader,
uint64_t address,
uint64_t size) {
INITIALIZATION_STATE_SET_INITIALIZING(initialized_);
process_reader_ = process_reader;
address_ = address;
size_ = size;
INITIALIZATION_STATE_SET_VALID(initialized_);
}
uint64_t MemorySnapshotMac::Address() const {
INITIALIZATION_STATE_DCHECK_VALID(initialized_);
return address_;
}
size_t MemorySnapshotMac::Size() const {
INITIALIZATION_STATE_DCHECK_VALID(initialized_);
return size_;
}
bool MemorySnapshotMac::Read(Delegate* delegate) const {
INITIALIZATION_STATE_DCHECK_VALID(initialized_);
if (size_ == 0) {
return delegate->MemorySnapshotDelegateRead(nullptr, size_);
}
std::unique_ptr<uint8_t[]> buffer(new uint8_t[size_]);
if (!process_reader_->Memory()->Read(address_, size_, buffer.get())) {
return false;
}
return delegate->MemorySnapshotDelegateRead(buffer.get(), size_);
}
const MemorySnapshot* MemorySnapshotMac::MergeWithOtherSnapshot(
const MemorySnapshot* other) const {
return MergeWithOtherSnapshotImpl(this, other);
}
} // namespace internal
} // namespace crashpad

View File

@ -21,8 +21,9 @@
#include "base/macros.h"
#include "build/build_config.h"
#include "snapshot/cpu_context.h"
#include "snapshot/mac/memory_snapshot_mac.h"
#include "snapshot/mac/process_reader.h"
#include "snapshot/memory_snapshot.h"
#include "snapshot/memory_snapshot_generic.h"
#include "snapshot/thread_snapshot.h"
#include "util/misc/initialization_state_dcheck.h"
@ -69,7 +70,7 @@ class ThreadSnapshotMac final : public ThreadSnapshot {
} context_union_;
#endif
CPUContext context_;
MemorySnapshotMac stack_;
MemorySnapshotGeneric<ProcessReader> stack_;
uint64_t thread_id_;
uint64_t thread_specific_data_address_;
thread_t thread_;

View File

@ -12,26 +12,29 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef CRASHPAD_SNAPSHOT_MAC_MEMORY_SNAPSHOT_MAC_H_
#define CRASHPAD_SNAPSHOT_MAC_MEMORY_SNAPSHOT_MAC_H_
#ifndef CRASHPAD_SNAPSHOT_MEMORY_SNAPSHOT_GENERIC_H_
#define CRASHPAD_SNAPSHOT_MEMORY_SNAPSHOT_GENERIC_H_
#include <stdint.h>
#include <sys/types.h>
#include "base/macros.h"
#include "snapshot/mac/process_reader.h"
#include "snapshot/memory_snapshot.h"
#include "util/misc/address_types.h"
#include "util/misc/initialization_state_dcheck.h"
#include "util/process/process_memory.h"
namespace crashpad {
namespace internal {
//! \brief A MemorySnapshot of a memory region in a process on the running
//! system, when the system runs macOS.
class MemorySnapshotMac final : public MemorySnapshot {
//! system. Used on Mac, Linux, Android, and Fuchsia, templated on the
//! platform-specific ProcessReader type.
template <class ProcessReaderType>
class MemorySnapshotGeneric final : public MemorySnapshot {
public:
MemorySnapshotMac();
~MemorySnapshotMac() override;
MemorySnapshotGeneric() = default;
~MemorySnapshotGeneric() = default;
//! \brief Initializes the object.
//!
@ -43,17 +46,46 @@ class MemorySnapshotMac final : public MemorySnapshot {
//! \param[in] address The base address of the memory region to snapshot, in
//! the snapshot process address space.
//! \param[in] size The size of the memory region to snapshot.
void Initialize(ProcessReader* process_reader,
uint64_t address,
uint64_t size);
void Initialize(ProcessReaderType* process_reader,
VMAddress address,
VMSize size) {
INITIALIZATION_STATE_SET_INITIALIZING(initialized_);
process_reader_ = process_reader;
address_ = address;
size_ = size;
INITIALIZATION_STATE_SET_VALID(initialized_);
}
// MemorySnapshot:
uint64_t Address() const override;
size_t Size() const override;
bool Read(Delegate* delegate) const override;
uint64_t Address() const override {
INITIALIZATION_STATE_DCHECK_VALID(initialized_);
return address_;
}
size_t Size() const override {
INITIALIZATION_STATE_DCHECK_VALID(initialized_);
return size_;
}
bool Read(Delegate* delegate) const override {
INITIALIZATION_STATE_DCHECK_VALID(initialized_);
if (size_ == 0) {
return delegate->MemorySnapshotDelegateRead(nullptr, size_);
}
std::unique_ptr<uint8_t[]> buffer(new uint8_t[size_]);
if (!process_reader_->Memory()->Read(address_, size_, buffer.get())) {
return false;
}
return delegate->MemorySnapshotDelegateRead(buffer.get(), size_);
}
const MemorySnapshot* MergeWithOtherSnapshot(
const MemorySnapshot* other) const override;
const MemorySnapshot* other) const override {
return MergeWithOtherSnapshotImpl(this, other);
}
private:
template <class T>
@ -61,15 +93,15 @@ class MemorySnapshotMac final : public MemorySnapshot {
const T* self,
const MemorySnapshot* other);
ProcessReader* process_reader_; // weak
ProcessReaderType* process_reader_; // weak
uint64_t address_;
uint64_t size_;
InitializationStateDcheck initialized_;
DISALLOW_COPY_AND_ASSIGN(MemorySnapshotMac);
DISALLOW_COPY_AND_ASSIGN(MemorySnapshotGeneric);
};
} // namespace internal
} // namespace crashpad
#endif // CRASHPAD_SNAPSHOT_MAC_MEMORY_SNAPSHOT_MAC_H_
#endif // CRASHPAD_SNAPSHOT_GENERIC_MEMORY_SNAPSHOT_GENERIC_H_

View File

@ -60,8 +60,6 @@
'linux/debug_rendezvous.h',
'linux/exception_snapshot_linux.cc',
'linux/exception_snapshot_linux.h',
'linux/memory_snapshot_linux.cc',
'linux/memory_snapshot_linux.h',
'linux/process_reader.cc',
'linux/process_reader.h',
'linux/process_snapshot_linux.cc',
@ -83,8 +81,6 @@
'mac/mach_o_image_segment_reader.h',
'mac/mach_o_image_symbol_table_reader.cc',
'mac/mach_o_image_symbol_table_reader.h',
'mac/memory_snapshot_mac.cc',
'mac/memory_snapshot_mac.h',
'mac/module_snapshot_mac.cc',
'mac/module_snapshot_mac.h',
'mac/process_reader.cc',
@ -110,6 +106,7 @@
'mac/thread_snapshot_mac.h',
'memory_snapshot.cc',
'memory_snapshot.h',
'memory_snapshot_generic.h',
'minidump/minidump_annotation_reader.cc',
'minidump/minidump_annotation_reader.h',
'minidump/minidump_simple_string_dictionary_reader.cc',