OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #include "snapshot/mac/module_snapshot_mac.h" |
| 16 |
| 17 #include <mach/mach.h> |
| 18 #include <mach-o/loader.h> |
| 19 |
| 20 #include "base/strings/stringprintf.h" |
| 21 #include "snapshot/mac/mach_o_image_annotations_reader.h" |
| 22 #include "snapshot/mac/mach_o_image_reader.h" |
| 23 #include "util/misc/uuid.h" |
| 24 #include "util/stdlib/strnlen.h" |
| 25 |
| 26 namespace crashpad { |
| 27 namespace internal { |
| 28 |
| 29 ModuleSnapshotMac::ModuleSnapshotMac() |
| 30 : ModuleSnapshot(), |
| 31 name_(), |
| 32 timestamp_(0), |
| 33 mach_o_image_reader_(nullptr), |
| 34 process_reader_(nullptr), |
| 35 initialized_() { |
| 36 } |
| 37 |
| 38 ModuleSnapshotMac::~ModuleSnapshotMac() { |
| 39 } |
| 40 |
| 41 bool ModuleSnapshotMac::Initialize( |
| 42 ProcessReader* process_reader, |
| 43 const ProcessReader::Module& process_reader_module) { |
| 44 INITIALIZATION_STATE_SET_INITIALIZING(initialized_); |
| 45 |
| 46 process_reader_ = process_reader; |
| 47 name_ = process_reader_module.name; |
| 48 timestamp_ = process_reader_module.timestamp; |
| 49 mach_o_image_reader_ = process_reader_module.reader; |
| 50 if (!mach_o_image_reader_) { |
| 51 return false; |
| 52 } |
| 53 |
| 54 INITIALIZATION_STATE_SET_VALID(initialized_); |
| 55 return true; |
| 56 } |
| 57 |
| 58 std::string ModuleSnapshotMac::Name() const { |
| 59 INITIALIZATION_STATE_DCHECK_VALID(initialized_); |
| 60 return name_; |
| 61 } |
| 62 |
| 63 uint64_t ModuleSnapshotMac::Address() const { |
| 64 INITIALIZATION_STATE_DCHECK_VALID(initialized_); |
| 65 return mach_o_image_reader_->Address(); |
| 66 } |
| 67 |
| 68 uint64_t ModuleSnapshotMac::Size() const { |
| 69 INITIALIZATION_STATE_DCHECK_VALID(initialized_); |
| 70 return mach_o_image_reader_->Size(); |
| 71 } |
| 72 |
| 73 time_t ModuleSnapshotMac::Timestamp() const { |
| 74 INITIALIZATION_STATE_DCHECK_VALID(initialized_); |
| 75 return timestamp_; |
| 76 } |
| 77 |
| 78 void ModuleSnapshotMac::FileVersion(uint16_t* version_0, |
| 79 uint16_t* version_1, |
| 80 uint16_t* version_2, |
| 81 uint16_t* version_3) const { |
| 82 INITIALIZATION_STATE_DCHECK_VALID(initialized_); |
| 83 if (mach_o_image_reader_->FileType() == MH_DYLIB) { |
| 84 uint32_t dylib_version = mach_o_image_reader_->DylibVersion(); |
| 85 *version_0 = (dylib_version & 0xffff0000) >> 16; |
| 86 *version_1 = (dylib_version & 0x0000ff00) >> 8; |
| 87 *version_2 = (dylib_version & 0x000000ff); |
| 88 *version_3 = 0; |
| 89 } else { |
| 90 *version_0 = 0; |
| 91 *version_1 = 0; |
| 92 *version_2 = 0; |
| 93 *version_3 = 0; |
| 94 } |
| 95 } |
| 96 |
| 97 void ModuleSnapshotMac::SourceVersion(uint16_t* version_0, |
| 98 uint16_t* version_1, |
| 99 uint16_t* version_2, |
| 100 uint16_t* version_3) const { |
| 101 INITIALIZATION_STATE_DCHECK_VALID(initialized_); |
| 102 |
| 103 // LC_SOURCE_VERSION is supposed to be interpreted as a 5-component version |
| 104 // number, 24 bits for the first component and 10 for the others, per |
| 105 // <mach-o/loader.h>. To preserve the full range of possible version numbers |
| 106 // without data loss, map it to the 4 16-bit fields mandated by the interface |
| 107 // here, which was informed by the minidump file format. |
| 108 uint64_t source_version = mach_o_image_reader_->SourceVersion(); |
| 109 *version_0 = (source_version & 0xffff000000000000u) >> 48; |
| 110 *version_1 = (source_version & 0x0000ffff00000000u) >> 32; |
| 111 *version_2 = (source_version & 0x00000000ffff0000u) >> 16; |
| 112 *version_3 = source_version & 0x000000000000ffffu; |
| 113 } |
| 114 |
| 115 ModuleSnapshot::ModuleType ModuleSnapshotMac::GetModuleType() const { |
| 116 INITIALIZATION_STATE_DCHECK_VALID(initialized_); |
| 117 |
| 118 uint32_t file_type = mach_o_image_reader_->FileType(); |
| 119 switch (file_type) { |
| 120 case MH_EXECUTE: |
| 121 return kModuleTypeExecutable; |
| 122 case MH_DYLIB: |
| 123 return kModuleTypeSharedLibrary; |
| 124 case MH_DYLINKER: |
| 125 return kModuleTypeDynamicLoader; |
| 126 case MH_BUNDLE: |
| 127 return kModuleTypeLoadableModule; |
| 128 default: |
| 129 return kModuleTypeUnknown; |
| 130 } |
| 131 } |
| 132 |
| 133 void ModuleSnapshotMac::UUID(crashpad::UUID* uuid) const { |
| 134 INITIALIZATION_STATE_DCHECK_VALID(initialized_); |
| 135 return mach_o_image_reader_->UUID(uuid); |
| 136 } |
| 137 |
| 138 std::vector<std::string> ModuleSnapshotMac::AnnotationsVector() const { |
| 139 MachOImageAnnotationsReader annotations_reader( |
| 140 process_reader_, mach_o_image_reader_, name_); |
| 141 return annotations_reader.Vector(); |
| 142 } |
| 143 |
| 144 std::map<std::string, std::string> ModuleSnapshotMac::AnnotationsSimpleMap() |
| 145 const { |
| 146 MachOImageAnnotationsReader annotations_reader( |
| 147 process_reader_, mach_o_image_reader_, name_); |
| 148 return annotations_reader.SimpleMap(); |
| 149 } |
| 150 |
| 151 } // namespace internal |
| 152 } // namespace crashpad |
OLD | NEW |