From 6584361343aa65a2b38f491687d25ee4ef6f618f Mon Sep 17 00:00:00 2001 From: Mark Mentovai Date: Fri, 17 Oct 2014 13:56:31 -0400 Subject: [PATCH] Add ModuleSnapshotMac. R=rsesek@chromium.org Review URL: https://codereview.chromium.org/649633004 --- snapshot/mac/module_snapshot_mac.cc | 152 ++++++++++++++++++++++++++++ snapshot/mac/module_snapshot_mac.h | 88 ++++++++++++++++ snapshot/mac/thread_snapshot_mac.h | 4 +- snapshot/module_snapshot.h | 48 +++++---- snapshot/snapshot.gyp | 2 + 5 files changed, 271 insertions(+), 23 deletions(-) create mode 100644 snapshot/mac/module_snapshot_mac.cc create mode 100644 snapshot/mac/module_snapshot_mac.h diff --git a/snapshot/mac/module_snapshot_mac.cc b/snapshot/mac/module_snapshot_mac.cc new file mode 100644 index 00000000..a290908c --- /dev/null +++ b/snapshot/mac/module_snapshot_mac.cc @@ -0,0 +1,152 @@ +// 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/module_snapshot_mac.h" + +#include +#include + +#include "base/strings/stringprintf.h" +#include "snapshot/mac/mach_o_image_annotations_reader.h" +#include "snapshot/mac/mach_o_image_reader.h" +#include "util/misc/uuid.h" +#include "util/stdlib/strnlen.h" + +namespace crashpad { +namespace internal { + +ModuleSnapshotMac::ModuleSnapshotMac() + : ModuleSnapshot(), + name_(), + timestamp_(0), + mach_o_image_reader_(nullptr), + process_reader_(nullptr), + initialized_() { +} + +ModuleSnapshotMac::~ModuleSnapshotMac() { +} + +bool ModuleSnapshotMac::Initialize( + ProcessReader* process_reader, + const ProcessReader::Module& process_reader_module) { + INITIALIZATION_STATE_SET_INITIALIZING(initialized_); + + process_reader_ = process_reader; + name_ = process_reader_module.name; + timestamp_ = process_reader_module.timestamp; + mach_o_image_reader_ = process_reader_module.reader; + if (!mach_o_image_reader_) { + return false; + } + + INITIALIZATION_STATE_SET_VALID(initialized_); + return true; +} + +std::string ModuleSnapshotMac::Name() const { + INITIALIZATION_STATE_DCHECK_VALID(initialized_); + return name_; +} + +uint64_t ModuleSnapshotMac::Address() const { + INITIALIZATION_STATE_DCHECK_VALID(initialized_); + return mach_o_image_reader_->Address(); +} + +uint64_t ModuleSnapshotMac::Size() const { + INITIALIZATION_STATE_DCHECK_VALID(initialized_); + return mach_o_image_reader_->Size(); +} + +time_t ModuleSnapshotMac::Timestamp() const { + INITIALIZATION_STATE_DCHECK_VALID(initialized_); + return timestamp_; +} + +void ModuleSnapshotMac::FileVersion(uint16_t* version_0, + uint16_t* version_1, + uint16_t* version_2, + uint16_t* version_3) const { + INITIALIZATION_STATE_DCHECK_VALID(initialized_); + if (mach_o_image_reader_->FileType() == MH_DYLIB) { + uint32_t dylib_version = mach_o_image_reader_->DylibVersion(); + *version_0 = (dylib_version & 0xffff0000) >> 16; + *version_1 = (dylib_version & 0x0000ff00) >> 8; + *version_2 = (dylib_version & 0x000000ff); + *version_3 = 0; + } else { + *version_0 = 0; + *version_1 = 0; + *version_2 = 0; + *version_3 = 0; + } +} + +void ModuleSnapshotMac::SourceVersion(uint16_t* version_0, + uint16_t* version_1, + uint16_t* version_2, + uint16_t* version_3) const { + INITIALIZATION_STATE_DCHECK_VALID(initialized_); + + // LC_SOURCE_VERSION is supposed to be interpreted as a 5-component version + // number, 24 bits for the first component and 10 for the others, per + // . To preserve the full range of possible version numbers + // without data loss, map it to the 4 16-bit fields mandated by the interface + // here, which was informed by the minidump file format. + uint64_t source_version = mach_o_image_reader_->SourceVersion(); + *version_0 = (source_version & 0xffff000000000000u) >> 48; + *version_1 = (source_version & 0x0000ffff00000000u) >> 32; + *version_2 = (source_version & 0x00000000ffff0000u) >> 16; + *version_3 = source_version & 0x000000000000ffffu; +} + +ModuleSnapshot::ModuleType ModuleSnapshotMac::GetModuleType() const { + INITIALIZATION_STATE_DCHECK_VALID(initialized_); + + uint32_t file_type = mach_o_image_reader_->FileType(); + switch (file_type) { + case MH_EXECUTE: + return kModuleTypeExecutable; + case MH_DYLIB: + return kModuleTypeSharedLibrary; + case MH_DYLINKER: + return kModuleTypeDynamicLoader; + case MH_BUNDLE: + return kModuleTypeLoadableModule; + default: + return kModuleTypeUnknown; + } +} + +void ModuleSnapshotMac::UUID(crashpad::UUID* uuid) const { + INITIALIZATION_STATE_DCHECK_VALID(initialized_); + return mach_o_image_reader_->UUID(uuid); +} + +std::vector ModuleSnapshotMac::AnnotationsVector() const { + MachOImageAnnotationsReader annotations_reader( + process_reader_, mach_o_image_reader_, name_); + return annotations_reader.Vector(); +} + +std::map ModuleSnapshotMac::AnnotationsSimpleMap() + const { + MachOImageAnnotationsReader annotations_reader( + process_reader_, mach_o_image_reader_, name_); + return annotations_reader.SimpleMap(); +} + +} // namespace internal +} // namespace crashpad diff --git a/snapshot/mac/module_snapshot_mac.h b/snapshot/mac/module_snapshot_mac.h new file mode 100644 index 00000000..11cc7f8c --- /dev/null +++ b/snapshot/mac/module_snapshot_mac.h @@ -0,0 +1,88 @@ +// 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. + +#ifndef CRASHPAD_SNAPSHOT_MAC_MODULE_SNAPSHOT_MAC_H_ +#define CRASHPAD_SNAPSHOT_MAC_MODULE_SNAPSHOT_MAC_H_ + +#include +#include + +#include +#include +#include + +#include "base/basictypes.h" +#include "snapshot/mac/process_reader.h" +#include "snapshot/module_snapshot.h" +#include "util/misc/initialization_state_dcheck.h" + +namespace crashpad { + +class MachOImageReader; +struct UUID; + +namespace internal { + +//! \brief A ModuleSnapshot of a code module (binary image) loaded into a +//! running (or crashed) process on a Mac OS X system. +class ModuleSnapshotMac final : public ModuleSnapshot { + public: + ModuleSnapshotMac(); + ~ModuleSnapshotMac(); + + //! \brief Initializes the object. + //! + //! \param[in] process_reader A ProcessReader for the task containing the + //! module. + //! \param[in] process_reader_module The module within the ProcessReader for + //! which the snapshot should be created. + //! + //! \return `true` if the snapshot could be created, `false` otherwise with + //! an appropriate message logged. + bool Initialize(ProcessReader* process_reader, + const ProcessReader::Module& process_reader_module); + + // ModuleSnapshot: + + std::string Name() const override; + uint64_t Address() const override; + uint64_t Size() const override; + time_t Timestamp() const override; + void FileVersion(uint16_t* version_0, + uint16_t* version_1, + uint16_t* version_2, + uint16_t* version_3) const override; + void SourceVersion(uint16_t* version_0, + uint16_t* version_1, + uint16_t* version_2, + uint16_t* version_3) const override; + ModuleType GetModuleType() const override; + void UUID(crashpad::UUID* uuid) const override; + std::vector AnnotationsVector() const override; + std::map AnnotationsSimpleMap() const override; + + private: + std::string name_; + time_t timestamp_; + const MachOImageReader* mach_o_image_reader_; // weak + ProcessReader* process_reader_; // weak + InitializationStateDcheck initialized_; + + DISALLOW_COPY_AND_ASSIGN(ModuleSnapshotMac); +}; + +} // namespace internal +} // namespace crashpad + +#endif // CRASHPAD_SNAPSHOT_MAC_MODULE_SNAPSHOT_MAC_H_ diff --git a/snapshot/mac/thread_snapshot_mac.h b/snapshot/mac/thread_snapshot_mac.h index 1a9634aa..2467944e 100644 --- a/snapshot/mac/thread_snapshot_mac.h +++ b/snapshot/mac/thread_snapshot_mac.h @@ -43,8 +43,8 @@ class ThreadSnapshotMac final : public ThreadSnapshot { //! //! \param[in] process_reader A ProcessReader for the task containing the //! thread. - //! \param[in] process_reader_thread The thread within the ProcessReader that - //! for which the snapshot should be created. + //! \param[in] process_reader_thread The thread within the ProcessReader for + //! which the snapshot should be created. //! //! \return `true` if the snapshot could be created, `false` otherwise with //! an appropriate message logged. diff --git a/snapshot/module_snapshot.h b/snapshot/module_snapshot.h index 7ebef044..b7e27707 100644 --- a/snapshot/module_snapshot.h +++ b/snapshot/module_snapshot.h @@ -113,32 +113,38 @@ class ModuleSnapshot { //! module does not have a UUID, the \a uuid parameter will be zeroed out. virtual void UUID(crashpad::UUID* uuid) const = 0; - //! \brief Returns diagnostic messages recorded in the module. + //! \brief Returns string annotations recorded in the module. //! - //! This method retrieves diagnostic messages recorded in a module. These - //! messages are intended for diagnostic use, including crash analysis. A - //! module may contain multiple diagnostic messages. + //! This method retrieves annotations recorded in a module. These annotations + //! are intended for diagnostic use, including crash analysis. A module may + //! contain multiple annotations, so they are returned in a vector. //! - //! For Mac OS X snapshots, the diagnostic messages are found by interpreting - //! the module’s `__DATA, __crash_info` section as - //! `crashreporter_annotations_t`. System libraries using the crash reporter - //! client interface may reference diagnostic messages in this structure. - //! Additional diagnostic messages may be found in other locations, which may - //! be module-specific. The dynamic linker (`dyld`) can provide a diagnostic - //! message at its `_error_string` symbol. - virtual std::vector DiagnosticMessages() const = 0; + //! For Mac OS X snapshots, these annotations are found by interpreting the + //! module’s `__DATA, __crash_info` section as `crashreporter_annotations_t`. + //! System libraries using the crash reporter client interface may reference + //! annotations in this structure. Additional annotations messages may be + //! found in other locations, which may be module-specific. The dynamic linker + //! (`dyld`) can provide an annotation at its `_error_string` symbol. + //! + //! The annotations returned by this method do not duplicate those returned by + //! AnnotationsSimpleMap(). + virtual std::vector AnnotationsVector() const = 0; - //! \brief Returns simple annotations recorded in the module. + //! \brief Returns key-value string annotations recorded in the module. //! - //! This method retrieves simple annotations recorded in a module. These - //! annotations are intended for diagnostic use, including crash analysis. - //! Simple annotations are structured as a sequence of key-value pairs. These - //! are referred to in Chrome as “crash keys.” + //! This method retrieves annotations recorded in a module. These annotations + //! are intended for diagnostic use, including crash analysis. “Simple + //! annotations” are structured as a sequence of key-value pairs, where all + //! keys and values are strings. These are referred to in Chrome as “crash + //! keys.” //! - //! For Mac OS X snapshots, simple annotations are found by interpreting - //! the `__DATA, __crashpad_info` section as `CrashpadInfo`. Clients can use - //! the Crashpad client interface to store annotations in this structure. - virtual std::map SimpleAnnotations() const = 0; + //! For Mac OS X snapshots, these annotations are found by interpreting the + //! `__DATA, __crashpad_info` section as `CrashpadInfo`. Clients can use the + //! Crashpad client interface to store annotations in this structure. + //! + //! The annotations returned by this method do not duplicate those returned by + //! AnnotationsVector(). + virtual std::map AnnotationsSimpleMap() const = 0; protected: ~ModuleSnapshot() {} diff --git a/snapshot/snapshot.gyp b/snapshot/snapshot.gyp index 196b1652..1792f890 100644 --- a/snapshot/snapshot.gyp +++ b/snapshot/snapshot.gyp @@ -45,6 +45,8 @@ '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', 'mac/process_reader.h', 'mac/process_types.cc',