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 "minidump/minidump_memory_writer.h" |
| 16 |
| 17 #include "base/logging.h" |
| 18 #include "util/numeric/safe_assignment.h" |
| 19 |
| 20 namespace crashpad { |
| 21 |
| 22 const MINIDUMP_MEMORY_DESCRIPTOR* |
| 23 MinidumpMemoryWriter::MinidumpMemoryDescriptor() const { |
| 24 DCHECK_EQ(state(), kStateWritable); |
| 25 |
| 26 return &memory_descriptor_; |
| 27 } |
| 28 |
| 29 void MinidumpMemoryWriter::RegisterMemoryDescriptor( |
| 30 MINIDUMP_MEMORY_DESCRIPTOR* memory_descriptor) { |
| 31 DCHECK_LE(state(), kStateFrozen); |
| 32 |
| 33 registered_memory_descriptors_.push_back(memory_descriptor); |
| 34 RegisterLocationDescriptor(&memory_descriptor->Memory); |
| 35 } |
| 36 |
| 37 MinidumpMemoryWriter::MinidumpMemoryWriter() |
| 38 : MinidumpWritable(), |
| 39 memory_descriptor_(), |
| 40 registered_memory_descriptors_() { |
| 41 } |
| 42 |
| 43 bool MinidumpMemoryWriter::Freeze() { |
| 44 DCHECK_EQ(state(), kStateMutable); |
| 45 |
| 46 if (!MinidumpWritable::Freeze()) { |
| 47 return false; |
| 48 } |
| 49 |
| 50 RegisterMemoryDescriptor(&memory_descriptor_); |
| 51 |
| 52 return true; |
| 53 } |
| 54 |
| 55 size_t MinidumpMemoryWriter::Alignment() { |
| 56 DCHECK_GE(state(), kStateFrozen); |
| 57 |
| 58 return 16; |
| 59 } |
| 60 |
| 61 size_t MinidumpMemoryWriter::SizeOfObject() { |
| 62 DCHECK_GE(state(), kStateFrozen); |
| 63 |
| 64 return MemoryRangeSize(); |
| 65 } |
| 66 |
| 67 bool MinidumpMemoryWriter::WillWriteAtOffsetImpl(off_t offset) { |
| 68 DCHECK_EQ(state(), kStateFrozen); |
| 69 |
| 70 // There will always be at least one registered descriptor, the one for this |
| 71 // object’s own memory_descriptor_ field. |
| 72 DCHECK_GE(registered_memory_descriptors_.size(), 1u); |
| 73 |
| 74 uint64_t base_address = MemoryRangeBaseAddress(); |
| 75 typeof(registered_memory_descriptors_[0]->StartOfMemoryRange) local_address; |
| 76 if (!AssignIfInRange(&local_address, base_address)) { |
| 77 LOG(ERROR) << "base_address " << base_address << " out of range"; |
| 78 return false; |
| 79 } |
| 80 |
| 81 for (MINIDUMP_MEMORY_DESCRIPTOR* memory_descriptor : |
| 82 registered_memory_descriptors_) { |
| 83 memory_descriptor->StartOfMemoryRange = local_address; |
| 84 } |
| 85 |
| 86 return MinidumpWritable::WillWriteAtOffsetImpl(offset); |
| 87 } |
| 88 |
| 89 internal::MinidumpWritable::Phase MinidumpMemoryWriter::WritePhase() { |
| 90 // Memory dumps are large and are unlikely to be consumed in their entirety. |
| 91 // Data accesses are expected to be sparse and sporadic, and are expected to |
| 92 // occur after all of the other structural and informational data from the |
| 93 // minidump file has been read. Put memory dumps at the end of the minidump |
| 94 // file to improve spatial locality. |
| 95 return kPhaseLate; |
| 96 } |
| 97 |
| 98 MinidumpMemoryListWriter::MinidumpMemoryListWriter() |
| 99 : MinidumpStreamWriter(), |
| 100 memory_list_base_(), |
| 101 memory_writers_(), |
| 102 children_() { |
| 103 } |
| 104 |
| 105 MinidumpMemoryListWriter::~MinidumpMemoryListWriter() { |
| 106 } |
| 107 |
| 108 void MinidumpMemoryListWriter::AddMemory(MinidumpMemoryWriter* memory_writer) { |
| 109 DCHECK_EQ(state(), kStateMutable); |
| 110 |
| 111 children_.push_back(memory_writer); |
| 112 AddExtraMemory(memory_writer); |
| 113 } |
| 114 |
| 115 void MinidumpMemoryListWriter::AddExtraMemory( |
| 116 MinidumpMemoryWriter* memory_writer) { |
| 117 DCHECK_EQ(state(), kStateMutable); |
| 118 |
| 119 memory_writers_.push_back(memory_writer); |
| 120 } |
| 121 |
| 122 bool MinidumpMemoryListWriter::Freeze() { |
| 123 DCHECK_EQ(state(), kStateMutable); |
| 124 |
| 125 if (!MinidumpStreamWriter::Freeze()) { |
| 126 return false; |
| 127 } |
| 128 |
| 129 size_t memory_region_count = memory_writers_.size(); |
| 130 CHECK_LE(children_.size(), memory_region_count); |
| 131 |
| 132 if (!AssignIfInRange(&memory_list_base_.NumberOfMemoryRanges, |
| 133 memory_region_count)) { |
| 134 LOG(ERROR) << "memory_region_count " << memory_region_count |
| 135 << " out of range"; |
| 136 return false; |
| 137 } |
| 138 |
| 139 return true; |
| 140 } |
| 141 |
| 142 size_t MinidumpMemoryListWriter::SizeOfObject() { |
| 143 DCHECK_GE(state(), kStateFrozen); |
| 144 DCHECK_LE(children_.size(), memory_writers_.size()); |
| 145 |
| 146 return sizeof(memory_list_base_) + |
| 147 memory_writers_.size() * sizeof(MINIDUMP_MEMORY_DESCRIPTOR); |
| 148 } |
| 149 |
| 150 std::vector<internal::MinidumpWritable*> MinidumpMemoryListWriter::Children() { |
| 151 DCHECK_GE(state(), kStateFrozen); |
| 152 DCHECK_LE(children_.size(), memory_writers_.size()); |
| 153 |
| 154 return children_; |
| 155 } |
| 156 |
| 157 bool MinidumpMemoryListWriter::WriteObject(FileWriterInterface* file_writer) { |
| 158 DCHECK_EQ(state(), kStateWritable); |
| 159 |
| 160 WritableIoVec iov; |
| 161 iov.iov_base = &memory_list_base_; |
| 162 iov.iov_len = sizeof(memory_list_base_); |
| 163 std::vector<WritableIoVec> iovecs(1, iov); |
| 164 |
| 165 for (const MinidumpMemoryWriter* memory_writer : memory_writers_) { |
| 166 iov.iov_len = sizeof(MINIDUMP_MEMORY_DESCRIPTOR); |
| 167 iov.iov_base = memory_writer->MinidumpMemoryDescriptor(); |
| 168 iovecs.push_back(iov); |
| 169 } |
| 170 |
| 171 return file_writer->WriteIoVec(&iovecs); |
| 172 } |
| 173 |
| 174 MinidumpStreamType MinidumpMemoryListWriter::StreamType() const { |
| 175 return kMinidumpStreamTypeMemoryList; |
| 176 } |
| 177 |
| 178 } // namespace crashpad |
OLD | NEW |