OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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_handle_writer.h" |
| 16 |
| 17 #include <string> |
| 18 |
| 19 #include "base/logging.h" |
| 20 #include "minidump/minidump_extensions.h" |
| 21 #include "util/file/file_writer.h" |
| 22 |
| 23 namespace crashpad { |
| 24 |
| 25 MinidumpHandleDataWriter::MinidumpHandleDataWriter() |
| 26 : handle_data_stream_base_(), handle_descriptors_(), strings_() { |
| 27 } |
| 28 |
| 29 MinidumpHandleDataWriter::~MinidumpHandleDataWriter() { |
| 30 } |
| 31 |
| 32 void MinidumpHandleDataWriter::InitializeFromSnapshot( |
| 33 const std::vector<HandleSnapshot>& handle_snapshots) { |
| 34 DCHECK_EQ(state(), kStateMutable); |
| 35 |
| 36 DCHECK(handle_descriptors_.empty()); |
| 37 // Because we RegisterRVA() on the string writer below, we preallocate and |
| 38 // never resize the handle_descriptors_ vector. |
| 39 handle_descriptors_.resize(handle_snapshots.size()); |
| 40 strings_.reserve(handle_snapshots.size()); |
| 41 for (size_t i = 0; i < handle_snapshots.size(); ++i) { |
| 42 const HandleSnapshot& handle_snapshot = handle_snapshots[i]; |
| 43 MINIDUMP_HANDLE_DESCRIPTOR& descriptor = handle_descriptors_[i]; |
| 44 |
| 45 descriptor.Handle = handle_snapshot.handle; |
| 46 |
| 47 if (handle_snapshot.type_name.empty()) { |
| 48 descriptor.TypeNameRva = 0; |
| 49 } else { |
| 50 // TODO(scottmg): There is often a number of repeated type names here, the |
| 51 // strings ought to be pooled. |
| 52 strings_.push_back(new internal::MinidumpUTF16StringWriter()); |
| 53 strings_.back()->SetUTF16(handle_snapshot.type_name); |
| 54 strings_.back()->RegisterRVA(&descriptor.TypeNameRva); |
| 55 } |
| 56 |
| 57 descriptor.ObjectNameRva = 0; |
| 58 descriptor.Attributes = handle_snapshot.attributes; |
| 59 descriptor.GrantedAccess = handle_snapshot.granted_access; |
| 60 descriptor.HandleCount = handle_snapshot.handle_count; |
| 61 descriptor.PointerCount = handle_snapshot.pointer_count; |
| 62 } |
| 63 } |
| 64 |
| 65 bool MinidumpHandleDataWriter::Freeze() { |
| 66 DCHECK_EQ(state(), kStateMutable); |
| 67 |
| 68 if (!MinidumpStreamWriter::Freeze()) |
| 69 return false; |
| 70 |
| 71 handle_data_stream_base_.SizeOfHeader = sizeof(handle_data_stream_base_); |
| 72 handle_data_stream_base_.SizeOfDescriptor = sizeof(handle_descriptors_[0]); |
| 73 handle_data_stream_base_.NumberOfDescriptors = handle_descriptors_.size(); |
| 74 handle_data_stream_base_.Reserved = 0; |
| 75 |
| 76 return true; |
| 77 } |
| 78 |
| 79 size_t MinidumpHandleDataWriter::SizeOfObject() { |
| 80 DCHECK_GE(state(), kStateFrozen); |
| 81 return sizeof(handle_data_stream_base_) + |
| 82 sizeof(handle_descriptors_[0]) * handle_descriptors_.size(); |
| 83 } |
| 84 |
| 85 std::vector<internal::MinidumpWritable*> MinidumpHandleDataWriter::Children() { |
| 86 DCHECK_GE(state(), kStateFrozen); |
| 87 |
| 88 std::vector<MinidumpWritable*> children; |
| 89 for (auto* string : strings_) |
| 90 children.push_back(string); |
| 91 return children; |
| 92 } |
| 93 |
| 94 bool MinidumpHandleDataWriter::WriteObject(FileWriterInterface* file_writer) { |
| 95 DCHECK_EQ(state(), kStateWritable); |
| 96 |
| 97 WritableIoVec iov; |
| 98 iov.iov_base = &handle_data_stream_base_; |
| 99 iov.iov_len = sizeof(handle_data_stream_base_); |
| 100 std::vector<WritableIoVec> iovecs(1, iov); |
| 101 |
| 102 for (const auto& descriptor : handle_descriptors_) { |
| 103 iov.iov_base = &descriptor; |
| 104 iov.iov_len = sizeof(descriptor); |
| 105 iovecs.push_back(iov); |
| 106 } |
| 107 |
| 108 return file_writer->WriteIoVec(&iovecs); |
| 109 } |
| 110 |
| 111 MinidumpStreamType MinidumpHandleDataWriter::StreamType() const { |
| 112 return kMinidumpStreamTypeHandleData; |
| 113 } |
| 114 |
| 115 } // namespace crashpad |
OLD | NEW |