1 //===-- SBMemoryRegionInfo.cpp ----------------------------------*- C++ -*-===//
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() {}
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::GetDescription(SBStream &description) {
120   LLDB_RECORD_METHOD(bool, SBMemoryRegionInfo, GetDescription,
121                      (lldb::SBStream &), description);
122 
123   Stream &strm = description.ref();
124   const addr_t load_addr = m_opaque_up->GetRange().base;
125 
126   strm.Printf("[0x%16.16" PRIx64 "-0x%16.16" PRIx64 " ", load_addr,
127               load_addr + m_opaque_up->GetRange().size);
128   strm.Printf(m_opaque_up->GetReadable() ? "R" : "-");
129   strm.Printf(m_opaque_up->GetWritable() ? "W" : "-");
130   strm.Printf(m_opaque_up->GetExecutable() ? "X" : "-");
131   strm.Printf("]");
132 
133   return true;
134 }
135 
136 namespace lldb_private {
137 namespace repro {
138 
139 template <>
140 void RegisterMethods<SBMemoryRegionInfo>(Registry &R) {
141   LLDB_REGISTER_CONSTRUCTOR(SBMemoryRegionInfo, ());
142   LLDB_REGISTER_CONSTRUCTOR(SBMemoryRegionInfo,
143                             (const lldb::SBMemoryRegionInfo &));
144   LLDB_REGISTER_METHOD(
145       const lldb::SBMemoryRegionInfo &,
146       SBMemoryRegionInfo, operator=,(const lldb::SBMemoryRegionInfo &));
147   LLDB_REGISTER_METHOD(void, SBMemoryRegionInfo, Clear, ());
148   LLDB_REGISTER_METHOD_CONST(
149       bool,
150       SBMemoryRegionInfo, operator==,(const lldb::SBMemoryRegionInfo &));
151   LLDB_REGISTER_METHOD_CONST(
152       bool,
153       SBMemoryRegionInfo, operator!=,(const lldb::SBMemoryRegionInfo &));
154   LLDB_REGISTER_METHOD(lldb::addr_t, SBMemoryRegionInfo, GetRegionBase, ());
155   LLDB_REGISTER_METHOD(lldb::addr_t, SBMemoryRegionInfo, GetRegionEnd, ());
156   LLDB_REGISTER_METHOD(bool, SBMemoryRegionInfo, IsReadable, ());
157   LLDB_REGISTER_METHOD(bool, SBMemoryRegionInfo, IsWritable, ());
158   LLDB_REGISTER_METHOD(bool, SBMemoryRegionInfo, IsExecutable, ());
159   LLDB_REGISTER_METHOD(bool, SBMemoryRegionInfo, IsMapped, ());
160   LLDB_REGISTER_METHOD(const char *, SBMemoryRegionInfo, GetName, ());
161   LLDB_REGISTER_METHOD(bool, SBMemoryRegionInfo, GetDescription,
162                        (lldb::SBStream &));
163 }
164 
165 }
166 }
167