xref: /freebsd-src/contrib/llvm-project/lldb/source/API/SBMemoryRegionInfoList.cpp (revision 5e801ac66d24704442eba426ed13c3effb8a34e7)
1 //===-- SBMemoryRegionInfoList.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/SBMemoryRegionInfoList.h"
10 #include "SBReproducerPrivate.h"
11 #include "lldb/API/SBMemoryRegionInfo.h"
12 #include "lldb/API/SBStream.h"
13 #include "lldb/Target/MemoryRegionInfo.h"
14 
15 #include <vector>
16 
17 using namespace lldb;
18 using namespace lldb_private;
19 
20 class MemoryRegionInfoListImpl {
21 public:
22   MemoryRegionInfoListImpl() : m_regions() {}
23 
24   MemoryRegionInfoListImpl(const MemoryRegionInfoListImpl &rhs)
25       : m_regions(rhs.m_regions) {}
26 
27   MemoryRegionInfoListImpl &operator=(const MemoryRegionInfoListImpl &rhs) {
28     if (this == &rhs)
29       return *this;
30     m_regions = rhs.m_regions;
31     return *this;
32   }
33 
34   size_t GetSize() const { return m_regions.size(); }
35 
36   void Reserve(size_t capacity) { return m_regions.reserve(capacity); }
37 
38   void Append(const MemoryRegionInfo &sb_region) {
39     m_regions.push_back(sb_region);
40   }
41 
42   void Append(const MemoryRegionInfoListImpl &list) {
43     Reserve(GetSize() + list.GetSize());
44 
45     for (const auto &val : list.m_regions)
46       Append(val);
47   }
48 
49   void Clear() { m_regions.clear(); }
50 
51   bool GetMemoryRegionContainingAddress(lldb::addr_t addr,
52                                         MemoryRegionInfo &region_info) {
53     for (auto &region : m_regions) {
54       if (region.GetRange().Contains(addr)) {
55         region_info = region;
56         return true;
57       }
58     }
59     return false;
60   }
61 
62   bool GetMemoryRegionInfoAtIndex(size_t index,
63                                   MemoryRegionInfo &region_info) {
64     if (index >= GetSize())
65       return false;
66     region_info = m_regions[index];
67     return true;
68   }
69 
70   MemoryRegionInfos &Ref() { return m_regions; }
71 
72   const MemoryRegionInfos &Ref() const { return m_regions; }
73 
74 private:
75   MemoryRegionInfos m_regions;
76 };
77 
78 MemoryRegionInfos &SBMemoryRegionInfoList::ref() { return m_opaque_up->Ref(); }
79 
80 const MemoryRegionInfos &SBMemoryRegionInfoList::ref() const {
81   return m_opaque_up->Ref();
82 }
83 
84 SBMemoryRegionInfoList::SBMemoryRegionInfoList()
85     : m_opaque_up(new MemoryRegionInfoListImpl()) {
86   LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBMemoryRegionInfoList);
87 }
88 
89 SBMemoryRegionInfoList::SBMemoryRegionInfoList(
90     const SBMemoryRegionInfoList &rhs)
91     : m_opaque_up(new MemoryRegionInfoListImpl(*rhs.m_opaque_up)) {
92   LLDB_RECORD_CONSTRUCTOR(SBMemoryRegionInfoList,
93                           (const lldb::SBMemoryRegionInfoList &), rhs);
94 }
95 
96 SBMemoryRegionInfoList::~SBMemoryRegionInfoList() = default;
97 
98 const SBMemoryRegionInfoList &SBMemoryRegionInfoList::
99 operator=(const SBMemoryRegionInfoList &rhs) {
100   LLDB_RECORD_METHOD(
101       const lldb::SBMemoryRegionInfoList &,
102       SBMemoryRegionInfoList, operator=,(const lldb::SBMemoryRegionInfoList &),
103       rhs);
104 
105   if (this != &rhs) {
106     *m_opaque_up = *rhs.m_opaque_up;
107   }
108   return LLDB_RECORD_RESULT(*this);
109 }
110 
111 uint32_t SBMemoryRegionInfoList::GetSize() const {
112   LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBMemoryRegionInfoList, GetSize);
113 
114   return m_opaque_up->GetSize();
115 }
116 
117 bool SBMemoryRegionInfoList::GetMemoryRegionContainingAddress(
118     lldb::addr_t addr, SBMemoryRegionInfo &region_info) {
119   LLDB_RECORD_METHOD(
120       bool, SBMemoryRegionInfoList, GetMemoryRegionContainingAddress,
121       (lldb::addr_t, lldb::SBMemoryRegionInfo &), addr, region_info);
122 
123   return m_opaque_up->GetMemoryRegionContainingAddress(addr, region_info.ref());
124 }
125 
126 bool SBMemoryRegionInfoList::GetMemoryRegionAtIndex(
127     uint32_t idx, SBMemoryRegionInfo &region_info) {
128   LLDB_RECORD_METHOD(bool, SBMemoryRegionInfoList, GetMemoryRegionAtIndex,
129                      (uint32_t, lldb::SBMemoryRegionInfo &), idx, region_info);
130 
131   return m_opaque_up->GetMemoryRegionInfoAtIndex(idx, region_info.ref());
132 }
133 
134 void SBMemoryRegionInfoList::Clear() {
135   LLDB_RECORD_METHOD_NO_ARGS(void, SBMemoryRegionInfoList, Clear);
136 
137   m_opaque_up->Clear();
138 }
139 
140 void SBMemoryRegionInfoList::Append(SBMemoryRegionInfo &sb_region) {
141   LLDB_RECORD_METHOD(void, SBMemoryRegionInfoList, Append,
142                      (lldb::SBMemoryRegionInfo &), sb_region);
143 
144   m_opaque_up->Append(sb_region.ref());
145 }
146 
147 void SBMemoryRegionInfoList::Append(SBMemoryRegionInfoList &sb_region_list) {
148   LLDB_RECORD_METHOD(void, SBMemoryRegionInfoList, Append,
149                      (lldb::SBMemoryRegionInfoList &), sb_region_list);
150 
151   m_opaque_up->Append(*sb_region_list);
152 }
153 
154 const MemoryRegionInfoListImpl *SBMemoryRegionInfoList::operator->() const {
155   return m_opaque_up.get();
156 }
157 
158 const MemoryRegionInfoListImpl &SBMemoryRegionInfoList::operator*() const {
159   assert(m_opaque_up.get());
160   return *m_opaque_up;
161 }
162 
163 namespace lldb_private {
164 namespace repro {
165 
166 template <>
167 void RegisterMethods<SBMemoryRegionInfoList>(Registry &R) {
168   LLDB_REGISTER_CONSTRUCTOR(SBMemoryRegionInfoList, ());
169   LLDB_REGISTER_CONSTRUCTOR(SBMemoryRegionInfoList,
170                             (const lldb::SBMemoryRegionInfoList &));
171   LLDB_REGISTER_METHOD(
172       const lldb::SBMemoryRegionInfoList &,
173       SBMemoryRegionInfoList, operator=,(
174                                   const lldb::SBMemoryRegionInfoList &));
175   LLDB_REGISTER_METHOD_CONST(uint32_t, SBMemoryRegionInfoList, GetSize, ());
176   LLDB_REGISTER_METHOD(bool, SBMemoryRegionInfoList,
177                        GetMemoryRegionContainingAddress,
178                        (lldb::addr_t, lldb::SBMemoryRegionInfo &));
179   LLDB_REGISTER_METHOD(bool, SBMemoryRegionInfoList, GetMemoryRegionAtIndex,
180                        (uint32_t, lldb::SBMemoryRegionInfo &));
181   LLDB_REGISTER_METHOD(void, SBMemoryRegionInfoList, Clear, ());
182   LLDB_REGISTER_METHOD(void, SBMemoryRegionInfoList, Append,
183                        (lldb::SBMemoryRegionInfo &));
184   LLDB_REGISTER_METHOD(void, SBMemoryRegionInfoList, Append,
185                        (lldb::SBMemoryRegionInfoList &));
186 }
187 
188 }
189 }
190