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_memory_info_writer.h"
16 
17 #include "base/check_op.h"
18 #include "snapshot/memory_map_region_snapshot.h"
19 #include "util/file/file_writer.h"
20 
21 namespace crashpad {
22 
MinidumpMemoryInfoListWriter()23 MinidumpMemoryInfoListWriter::MinidumpMemoryInfoListWriter()
24     : memory_info_list_base_(), items_() {
25 }
26 
~MinidumpMemoryInfoListWriter()27 MinidumpMemoryInfoListWriter::~MinidumpMemoryInfoListWriter() {
28 }
29 
InitializeFromSnapshot(const std::vector<const MemoryMapRegionSnapshot * > & memory_map)30 void MinidumpMemoryInfoListWriter::InitializeFromSnapshot(
31     const std::vector<const MemoryMapRegionSnapshot*>& memory_map) {
32   DCHECK_EQ(state(), kStateMutable);
33 
34   DCHECK(items_.empty());
35   for (const auto& region : memory_map)
36     items_.push_back(region->AsMinidumpMemoryInfo());
37 }
38 
Freeze()39 bool MinidumpMemoryInfoListWriter::Freeze() {
40   DCHECK_EQ(state(), kStateMutable);
41 
42   if (!MinidumpStreamWriter::Freeze())
43     return false;
44 
45   memory_info_list_base_.SizeOfHeader = sizeof(MINIDUMP_MEMORY_INFO_LIST);
46   memory_info_list_base_.SizeOfEntry = sizeof(MINIDUMP_MEMORY_INFO);
47   memory_info_list_base_.NumberOfEntries = items_.size();
48 
49   return true;
50 }
51 
SizeOfObject()52 size_t MinidumpMemoryInfoListWriter::SizeOfObject() {
53   DCHECK_GE(state(), kStateFrozen);
54   return sizeof(memory_info_list_base_) + sizeof(items_[0]) * items_.size();
55 }
56 
57 std::vector<internal::MinidumpWritable*>
Children()58 MinidumpMemoryInfoListWriter::Children() {
59   DCHECK_GE(state(), kStateFrozen);
60   return std::vector<internal::MinidumpWritable*>();
61 }
62 
WriteObject(FileWriterInterface * file_writer)63 bool MinidumpMemoryInfoListWriter::WriteObject(
64     FileWriterInterface* file_writer) {
65   DCHECK_EQ(state(), kStateWritable);
66 
67   WritableIoVec iov;
68   iov.iov_base = &memory_info_list_base_;
69   iov.iov_len = sizeof(memory_info_list_base_);
70   std::vector<WritableIoVec> iovecs(1, iov);
71 
72   for (const auto& minidump_memory_info : items_) {
73     iov.iov_base = &minidump_memory_info;
74     iov.iov_len = sizeof(minidump_memory_info);
75     iovecs.push_back(iov);
76   }
77 
78   return file_writer->WriteIoVec(&iovecs);
79 }
80 
StreamType() const81 MinidumpStreamType MinidumpMemoryInfoListWriter::StreamType() const {
82   return kMinidumpStreamTypeMemoryInfoList;
83 }
84 
85 }  // namespace crashpad
86