OLD | NEW |
1 // Copyright 2015 The Crashpad Authors. All rights reserved. | 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with 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 | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 #include "minidump/minidump_handle_writer.h" | 15 #include "minidump/minidump_handle_writer.h" |
16 | 16 |
17 #include <string> | 17 #include <string> |
18 | 18 |
19 #include "base/logging.h" | 19 #include "base/logging.h" |
| 20 #include "base/stl_util.h" |
20 #include "minidump/minidump_extensions.h" | 21 #include "minidump/minidump_extensions.h" |
21 #include "util/file/file_writer.h" | 22 #include "util/file/file_writer.h" |
22 | 23 |
23 namespace crashpad { | 24 namespace crashpad { |
24 | 25 |
25 MinidumpHandleDataWriter::MinidumpHandleDataWriter() | 26 MinidumpHandleDataWriter::MinidumpHandleDataWriter() |
26 : handle_data_stream_base_(), handle_descriptors_(), strings_() { | 27 : handle_data_stream_base_(), handle_descriptors_(), strings_() { |
27 } | 28 } |
28 | 29 |
29 MinidumpHandleDataWriter::~MinidumpHandleDataWriter() { | 30 MinidumpHandleDataWriter::~MinidumpHandleDataWriter() { |
| 31 STLDeleteContainerPairSecondPointers(strings_.begin(), strings_.end()); |
30 } | 32 } |
31 | 33 |
32 void MinidumpHandleDataWriter::InitializeFromSnapshot( | 34 void MinidumpHandleDataWriter::InitializeFromSnapshot( |
33 const std::vector<HandleSnapshot>& handle_snapshots) { | 35 const std::vector<HandleSnapshot>& handle_snapshots) { |
34 DCHECK_EQ(state(), kStateMutable); | 36 DCHECK_EQ(state(), kStateMutable); |
35 | 37 |
36 DCHECK(handle_descriptors_.empty()); | 38 DCHECK(handle_descriptors_.empty()); |
37 // Because we RegisterRVA() on the string writer below, we preallocate and | 39 // Because we RegisterRVA() on the string writer below, we preallocate and |
38 // never resize the handle_descriptors_ vector. | 40 // never resize the handle_descriptors_ vector. |
39 handle_descriptors_.resize(handle_snapshots.size()); | 41 handle_descriptors_.resize(handle_snapshots.size()); |
40 strings_.reserve(handle_snapshots.size()); | |
41 for (size_t i = 0; i < handle_snapshots.size(); ++i) { | 42 for (size_t i = 0; i < handle_snapshots.size(); ++i) { |
42 const HandleSnapshot& handle_snapshot = handle_snapshots[i]; | 43 const HandleSnapshot& handle_snapshot = handle_snapshots[i]; |
43 MINIDUMP_HANDLE_DESCRIPTOR& descriptor = handle_descriptors_[i]; | 44 MINIDUMP_HANDLE_DESCRIPTOR& descriptor = handle_descriptors_[i]; |
44 | 45 |
45 descriptor.Handle = handle_snapshot.handle; | 46 descriptor.Handle = handle_snapshot.handle; |
46 | 47 |
47 if (handle_snapshot.type_name.empty()) { | 48 if (handle_snapshot.type_name.empty()) { |
48 descriptor.TypeNameRva = 0; | 49 descriptor.TypeNameRva = 0; |
49 } else { | 50 } else { |
50 // TODO(scottmg): There is often a number of repeated type names here, the | 51 auto it = strings_.lower_bound(handle_snapshot.type_name); |
51 // strings ought to be pooled. | 52 internal::MinidumpUTF16StringWriter* writer; |
52 strings_.push_back(new internal::MinidumpUTF16StringWriter()); | 53 if (it != strings_.end() && it->first == handle_snapshot.type_name) { |
53 strings_.back()->SetUTF8(handle_snapshot.type_name); | 54 writer = it->second; |
54 strings_.back()->RegisterRVA(&descriptor.TypeNameRva); | 55 } else { |
| 56 writer = new internal::MinidumpUTF16StringWriter(); |
| 57 strings_.insert(it, std::make_pair(handle_snapshot.type_name, writer)); |
| 58 writer->SetUTF8(handle_snapshot.type_name); |
| 59 } |
| 60 writer->RegisterRVA(&descriptor.TypeNameRva); |
55 } | 61 } |
56 | 62 |
57 descriptor.ObjectNameRva = 0; | 63 descriptor.ObjectNameRva = 0; |
58 descriptor.Attributes = handle_snapshot.attributes; | 64 descriptor.Attributes = handle_snapshot.attributes; |
59 descriptor.GrantedAccess = handle_snapshot.granted_access; | 65 descriptor.GrantedAccess = handle_snapshot.granted_access; |
60 descriptor.HandleCount = handle_snapshot.handle_count; | 66 descriptor.HandleCount = handle_snapshot.handle_count; |
61 descriptor.PointerCount = handle_snapshot.pointer_count; | 67 descriptor.PointerCount = handle_snapshot.pointer_count; |
62 } | 68 } |
63 } | 69 } |
64 | 70 |
(...skipping 14 matching lines...) Expand all Loading... |
79 size_t MinidumpHandleDataWriter::SizeOfObject() { | 85 size_t MinidumpHandleDataWriter::SizeOfObject() { |
80 DCHECK_GE(state(), kStateFrozen); | 86 DCHECK_GE(state(), kStateFrozen); |
81 return sizeof(handle_data_stream_base_) + | 87 return sizeof(handle_data_stream_base_) + |
82 sizeof(handle_descriptors_[0]) * handle_descriptors_.size(); | 88 sizeof(handle_descriptors_[0]) * handle_descriptors_.size(); |
83 } | 89 } |
84 | 90 |
85 std::vector<internal::MinidumpWritable*> MinidumpHandleDataWriter::Children() { | 91 std::vector<internal::MinidumpWritable*> MinidumpHandleDataWriter::Children() { |
86 DCHECK_GE(state(), kStateFrozen); | 92 DCHECK_GE(state(), kStateFrozen); |
87 | 93 |
88 std::vector<MinidumpWritable*> children; | 94 std::vector<MinidumpWritable*> children; |
89 for (auto* string : strings_) | 95 for (const auto& pair : strings_) |
90 children.push_back(string); | 96 children.push_back(pair.second); |
91 return children; | 97 return children; |
92 } | 98 } |
93 | 99 |
94 bool MinidumpHandleDataWriter::WriteObject(FileWriterInterface* file_writer) { | 100 bool MinidumpHandleDataWriter::WriteObject(FileWriterInterface* file_writer) { |
95 DCHECK_EQ(state(), kStateWritable); | 101 DCHECK_EQ(state(), kStateWritable); |
96 | 102 |
97 WritableIoVec iov; | 103 WritableIoVec iov; |
98 iov.iov_base = &handle_data_stream_base_; | 104 iov.iov_base = &handle_data_stream_base_; |
99 iov.iov_len = sizeof(handle_data_stream_base_); | 105 iov.iov_len = sizeof(handle_data_stream_base_); |
100 std::vector<WritableIoVec> iovecs(1, iov); | 106 std::vector<WritableIoVec> iovecs(1, iov); |
101 | 107 |
102 for (const auto& descriptor : handle_descriptors_) { | 108 for (const auto& descriptor : handle_descriptors_) { |
103 iov.iov_base = &descriptor; | 109 iov.iov_base = &descriptor; |
104 iov.iov_len = sizeof(descriptor); | 110 iov.iov_len = sizeof(descriptor); |
105 iovecs.push_back(iov); | 111 iovecs.push_back(iov); |
106 } | 112 } |
107 | 113 |
108 return file_writer->WriteIoVec(&iovecs); | 114 return file_writer->WriteIoVec(&iovecs); |
109 } | 115 } |
110 | 116 |
111 MinidumpStreamType MinidumpHandleDataWriter::StreamType() const { | 117 MinidumpStreamType MinidumpHandleDataWriter::StreamType() const { |
112 return kMinidumpStreamTypeHandleData; | 118 return kMinidumpStreamTypeHandleData; |
113 } | 119 } |
114 | 120 |
115 } // namespace crashpad | 121 } // namespace crashpad |
OLD | NEW |