mirrored from https://chromium.googlesource.com/crashpad/crashpad
-
Notifications
You must be signed in to change notification settings - Fork 164
/
minidump_annotation_writer.cc
164 lines (124 loc) · 4.41 KB
/
minidump_annotation_writer.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2017 The Crashpad Authors
//
// 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 "minidump/minidump_annotation_writer.h"
#include <memory>
#include "base/check_op.h"
#include "base/logging.h"
#include "util/file/file_writer.h"
#include "util/numeric/safe_assignment.h"
namespace crashpad {
MinidumpAnnotationWriter::MinidumpAnnotationWriter() = default;
MinidumpAnnotationWriter::~MinidumpAnnotationWriter() = default;
void MinidumpAnnotationWriter::InitializeFromSnapshot(
const AnnotationSnapshot& snapshot) {
DCHECK_EQ(state(), kStateMutable);
name_.SetUTF8(snapshot.name);
annotation_.type = snapshot.type;
annotation_.reserved = 0;
value_.set_data(snapshot.value);
}
void MinidumpAnnotationWriter::InitializeWithData(
const std::string& name,
uint16_t type,
const std::vector<uint8_t>& data) {
DCHECK_EQ(state(), kStateMutable);
name_.SetUTF8(name);
annotation_.type = type;
annotation_.reserved = 0;
value_.set_data(data);
}
bool MinidumpAnnotationWriter::Freeze() {
DCHECK_EQ(state(), kStateMutable);
if (!MinidumpWritable::Freeze()) {
return false;
}
name_.RegisterRVA(&annotation_.name);
value_.RegisterRVA(&annotation_.value);
return true;
}
size_t MinidumpAnnotationWriter::SizeOfObject() {
DCHECK_GE(state(), kStateFrozen);
// This object is written by the MinidumpAnnotationListWriter, and its
// children write themselves.
return 0;
}
std::vector<internal::MinidumpWritable*> MinidumpAnnotationWriter::Children() {
DCHECK_GE(state(), kStateFrozen);
return {&name_, &value_};
}
bool MinidumpAnnotationWriter::WriteObject(FileWriterInterface* file_writer) {
DCHECK_EQ(state(), kStateWritable);
// This object is written by the MinidumpAnnotationListWriter, and its
// children write themselves.
return true;
}
MinidumpAnnotationListWriter::MinidumpAnnotationListWriter()
: minidump_list_(new MinidumpAnnotationList()) {}
MinidumpAnnotationListWriter::~MinidumpAnnotationListWriter() = default;
void MinidumpAnnotationListWriter::InitializeFromList(
const std::vector<AnnotationSnapshot>& list) {
DCHECK_EQ(state(), kStateMutable);
for (const auto& annotation : list) {
auto writer = std::make_unique<MinidumpAnnotationWriter>();
writer->InitializeFromSnapshot(annotation);
AddObject(std::move(writer));
}
}
void MinidumpAnnotationListWriter::AddObject(
std::unique_ptr<MinidumpAnnotationWriter> annotation_writer) {
DCHECK_EQ(state(), kStateMutable);
objects_.push_back(std::move(annotation_writer));
}
bool MinidumpAnnotationListWriter::IsUseful() const {
return !objects_.empty();
}
bool MinidumpAnnotationListWriter::Freeze() {
DCHECK_EQ(state(), kStateMutable);
if (!MinidumpWritable::Freeze()) {
return false;
}
if (!AssignIfInRange(&minidump_list_->count, objects_.size())) {
LOG(ERROR) << "annotation list size " << objects_.size()
<< " is out of range";
return false;
}
return true;
}
size_t MinidumpAnnotationListWriter::SizeOfObject() {
DCHECK_GE(state(), kStateFrozen);
return sizeof(*minidump_list_) + sizeof(MinidumpAnnotation) * objects_.size();
}
std::vector<internal::MinidumpWritable*>
MinidumpAnnotationListWriter::Children() {
DCHECK_GE(state(), kStateFrozen);
std::vector<internal::MinidumpWritable*> children(objects_.size());
for (size_t i = 0; i < objects_.size(); ++i) {
children[i] = objects_[i].get();
}
return children;
}
bool MinidumpAnnotationListWriter::WriteObject(
FileWriterInterface* file_writer) {
DCHECK_EQ(state(), kStateWritable);
std::vector<WritableIoVec> iov;
iov.reserve(1 + objects_.size());
iov.emplace_back(
WritableIoVec{minidump_list_.get(), sizeof(*minidump_list_)});
for (const auto& object : objects_) {
iov.emplace_back(WritableIoVec{object->minidump_annotation(),
sizeof(MinidumpAnnotation)});
}
return file_writer->WriteIoVec(&iov);
}
} // namespace crashpad