1 //===-- SBMemoryRegionInfo.cpp --------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/API/SBMemoryRegionInfo.h"
10 #include "SBReproducerPrivate.h"
11 #include "Utils.h"
12 #include "lldb/API/SBDefines.h"
13 #include "lldb/API/SBError.h"
14 #include "lldb/API/SBStream.h"
15 #include "lldb/Target/MemoryRegionInfo.h"
16 #include "lldb/Utility/StreamString.h"
17 
18 using namespace lldb;
19 using namespace lldb_private;
20 
21 SBMemoryRegionInfo::SBMemoryRegionInfo() : m_opaque_up(new MemoryRegionInfo()) {
22   LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBMemoryRegionInfo);
23 }
24 
25 SBMemoryRegionInfo::SBMemoryRegionInfo(const MemoryRegionInfo *lldb_object_ptr)
26     : m_opaque_up(new MemoryRegionInfo()) {
27   if (lldb_object_ptr)
28     ref() = *lldb_object_ptr;
29 }
30 
31 SBMemoryRegionInfo::SBMemoryRegionInfo(const SBMemoryRegionInfo &rhs)
32     : m_opaque_up() {
33   LLDB_RECORD_CONSTRUCTOR(SBMemoryRegionInfo,
34                           (const lldb::SBMemoryRegionInfo &), rhs);
35   m_opaque_up = clone(rhs.m_opaque_up);
36 }
37 
38 const SBMemoryRegionInfo &SBMemoryRegionInfo::
39 operator=(const SBMemoryRegionInfo &rhs) {
40   LLDB_RECORD_METHOD(
41       const lldb::SBMemoryRegionInfo &,
42       SBMemoryRegionInfo, operator=,(const lldb::SBMemoryRegionInfo &), rhs);
43 
44   if (this != &rhs)
45     m_opaque_up = clone(rhs.m_opaque_up);
46   return LLDB_RECORD_RESULT(*this);
47 }
48 
49 SBMemoryRegionInfo::~SBMemoryRegionInfo() = default;
50 
51 void SBMemoryRegionInfo::Clear() {
52   LLDB_RECORD_METHOD_NO_ARGS(void, SBMemoryRegionInfo, Clear);
53 
54   m_opaque_up->Clear();
55 }
56 
57 bool SBMemoryRegionInfo::operator==(const SBMemoryRegionInfo &rhs) const {
58   LLDB_RECORD_METHOD_CONST(
59       bool, SBMemoryRegionInfo, operator==,(const lldb::SBMemoryRegionInfo &),
60       rhs);
61 
62   return ref() == rhs.ref();
63 }
64 
65 bool SBMemoryRegionInfo::operator!=(const SBMemoryRegionInfo &rhs) const {
66   LLDB_RECORD_METHOD_CONST(
67       bool, SBMemoryRegionInfo, operator!=,(const lldb::SBMemoryRegionInfo &),
68       rhs);
69 
70   return ref() != rhs.ref();
71 }
72 
73 MemoryRegionInfo &SBMemoryRegionInfo::ref() { return *m_opaque_up; }
74 
75 const MemoryRegionInfo &SBMemoryRegionInfo::ref() const { return *m_opaque_up; }
76 
77 lldb::addr_t SBMemoryRegionInfo::GetRegionBase() {
78   LLDB_RECORD_METHOD_NO_ARGS(lldb::addr_t, SBMemoryRegionInfo, GetRegionBase);
79 
80   return m_opaque_up->GetRange().GetRangeBase();
81 }
82 
83 lldb::addr_t SBMemoryRegionInfo::GetRegionEnd() {
84   LLDB_RECORD_METHOD_NO_ARGS(lldb::addr_t, SBMemoryRegionInfo, GetRegionEnd);
85 
86   return m_opaque_up->GetRange().GetRangeEnd();
87 }
88 
89 bool SBMemoryRegionInfo::IsReadable() {
90   LLDB_RECORD_METHOD_NO_ARGS(bool, SBMemoryRegionInfo, IsReadable);
91 
92   return m_opaque_up->GetReadable() == MemoryRegionInfo::eYes;
93 }
94 
95 bool SBMemoryRegionInfo::IsWritable() {
96   LLDB_RECORD_METHOD_NO_ARGS(bool, SBMemoryRegionInfo, IsWritable);
97 
98   return m_opaque_up->GetWritable() == MemoryRegionInfo::eYes;
99 }
100 
101 bool SBMemoryRegionInfo::IsExecutable() {
102   LLDB_RECORD_METHOD_NO_ARGS(bool, SBMemoryRegionInfo, IsExecutable);
103 
104   return m_opaque_up->GetExecutable() == MemoryRegionInfo::eYes;
105 }
106 
107 bool SBMemoryRegionInfo::IsMapped() {
108   LLDB_RECORD_METHOD_NO_ARGS(bool, SBMemoryRegionInfo, IsMapped);
109 
110   return m_opaque_up->GetMapped() == MemoryRegionInfo::eYes;
111 }
112 
113 const char *SBMemoryRegionInfo::GetName() {
114   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBMemoryRegionInfo, GetName);
115 
116   return m_opaque_up->GetName().AsCString();
117 }
118 
119 bool SBMemoryRegionInfo::HasDirtyMemoryPageList() {
120   LLDB_RECORD_METHOD_NO_ARGS(bool, SBMemoryRegionInfo, HasDirtyMemoryPageList);
121 
122   return m_opaque_up->GetDirtyPageList().hasValue();
123 }
124 
125 uint32_t SBMemoryRegionInfo::GetNumDirtyPages() {
126   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBMemoryRegionInfo, GetNumDirtyPages);
127 
128   uint32_t num_dirty_pages = 0;
129   llvm::Optional<std::vector<addr_t>> dirty_page_list =
130       m_opaque_up->GetDirtyPageList();
131   if (dirty_page_list.hasValue())
132     num_dirty_pages = dirty_page_list.getValue().size();
133 
134   return num_dirty_pages;
135 }
136 
137 addr_t SBMemoryRegionInfo::GetDirtyPageAddressAtIndex(uint32_t idx) {
138   LLDB_RECORD_METHOD(addr_t, SBMemoryRegionInfo, GetDirtyPageAddressAtIndex,
139                      (uint32_t), idx);
140 
141   addr_t dirty_page_addr = LLDB_INVALID_ADDRESS;
142   const llvm::Optional<std::vector<addr_t>> &dirty_page_list =
143       m_opaque_up->GetDirtyPageList();
144   if (dirty_page_list.hasValue() && idx < dirty_page_list.getValue().size())
145     dirty_page_addr = dirty_page_list.getValue()[idx];
146 
147   return dirty_page_addr;
148 }
149 
150 int SBMemoryRegionInfo::GetPageSize() {
151   LLDB_RECORD_METHOD_NO_ARGS(int, SBMemoryRegionInfo, GetPageSize);
152   return m_opaque_up->GetPageSize();
153 }
154 
155 bool SBMemoryRegionInfo::GetDescription(SBStream &description) {
156   LLDB_RECORD_METHOD(bool, SBMemoryRegionInfo, GetDescription,
157                      (lldb::SBStream &), description);
158 
159   Stream &strm = description.ref();
160   const addr_t load_addr = m_opaque_up->GetRange().base;
161 
162   strm.Printf("[0x%16.16" PRIx64 "-0x%16.16" PRIx64 " ", load_addr,
163               load_addr + m_opaque_up->GetRange().size);
164   strm.Printf(m_opaque_up->GetReadable() ? "R" : "-");
165   strm.Printf(m_opaque_up->GetWritable() ? "W" : "-");
166   strm.Printf(m_opaque_up->GetExecutable() ? "X" : "-");
167   strm.Printf("]");
168 
169   return true;
170 }
171 
172 namespace lldb_private {
173 namespace repro {
174 
175 template <>
176 void RegisterMethods<SBMemoryRegionInfo>(Registry &R) {
177   LLDB_REGISTER_CONSTRUCTOR(SBMemoryRegionInfo, ());
178   LLDB_REGISTER_CONSTRUCTOR(SBMemoryRegionInfo,
179                             (const lldb::SBMemoryRegionInfo &));
180   LLDB_REGISTER_METHOD(
181       const lldb::SBMemoryRegionInfo &,
182       SBMemoryRegionInfo, operator=,(const lldb::SBMemoryRegionInfo &));
183   LLDB_REGISTER_METHOD(void, SBMemoryRegionInfo, Clear, ());
184   LLDB_REGISTER_METHOD_CONST(
185       bool,
186       SBMemoryRegionInfo, operator==,(const lldb::SBMemoryRegionInfo &));
187   LLDB_REGISTER_METHOD_CONST(
188       bool,
189       SBMemoryRegionInfo, operator!=,(const lldb::SBMemoryRegionInfo &));
190   LLDB_REGISTER_METHOD(lldb::addr_t, SBMemoryRegionInfo, GetRegionBase, ());
191   LLDB_REGISTER_METHOD(lldb::addr_t, SBMemoryRegionInfo, GetRegionEnd, ());
192   LLDB_REGISTER_METHOD(bool, SBMemoryRegionInfo, IsReadable, ());
193   LLDB_REGISTER_METHOD(bool, SBMemoryRegionInfo, IsWritable, ());
194   LLDB_REGISTER_METHOD(bool, SBMemoryRegionInfo, IsExecutable, ());
195   LLDB_REGISTER_METHOD(bool, SBMemoryRegionInfo, IsMapped, ());
196   LLDB_REGISTER_METHOD(const char *, SBMemoryRegionInfo, GetName, ());
197   LLDB_REGISTER_METHOD(bool, SBMemoryRegionInfo, GetDescription,
198                        (lldb::SBStream &));
199 }
200 
201 }
202 }
203