180814287SRaphael Isemann //===-- SBMemoryRegionInfo.cpp --------------------------------------------===//
226036843SHoward Hellyer //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
626036843SHoward Hellyer //
726036843SHoward Hellyer //===----------------------------------------------------------------------===//
826036843SHoward Hellyer 
9b9c1b51eSKate Stone #include "lldb/API/SBMemoryRegionInfo.h"
10bd4bf82aSJonas Devlieghere #include "Utils.h"
1126036843SHoward Hellyer #include "lldb/API/SBDefines.h"
1226036843SHoward Hellyer #include "lldb/API/SBError.h"
1326036843SHoward Hellyer #include "lldb/API/SBStream.h"
1426036843SHoward Hellyer #include "lldb/Target/MemoryRegionInfo.h"
151755f5b1SJonas Devlieghere #include "lldb/Utility/Instrumentation.h"
16bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h"
1726036843SHoward Hellyer 
1826036843SHoward Hellyer using namespace lldb;
1926036843SHoward Hellyer using namespace lldb_private;
2026036843SHoward Hellyer 
SBMemoryRegionInfo()21baf5664fSJonas Devlieghere SBMemoryRegionInfo::SBMemoryRegionInfo() : m_opaque_up(new MemoryRegionInfo()) {
221755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
23baf5664fSJonas Devlieghere }
2426036843SHoward Hellyer 
SBMemoryRegionInfo(const char * name,lldb::addr_t begin,lldb::addr_t end,uint32_t permissions,bool mapped,bool stack_memory)25a758c9f7SMed Ismail Bennani SBMemoryRegionInfo::SBMemoryRegionInfo(const char *name, lldb::addr_t begin,
26a758c9f7SMed Ismail Bennani                                        lldb::addr_t end, uint32_t permissions,
27a758c9f7SMed Ismail Bennani                                        bool mapped, bool stack_memory)
28a758c9f7SMed Ismail Bennani     : SBMemoryRegionInfo() {
291755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, name, begin, end, permissions, mapped, stack_memory);
30a758c9f7SMed Ismail Bennani   m_opaque_up->SetName(name);
31a758c9f7SMed Ismail Bennani   m_opaque_up->GetRange().SetRangeBase(begin);
32a758c9f7SMed Ismail Bennani   m_opaque_up->GetRange().SetRangeEnd(end);
33a758c9f7SMed Ismail Bennani   m_opaque_up->SetLLDBPermissions(permissions);
34a758c9f7SMed Ismail Bennani   m_opaque_up->SetMapped(mapped ? MemoryRegionInfo::eYes
35a758c9f7SMed Ismail Bennani                                 : MemoryRegionInfo::eNo);
36a758c9f7SMed Ismail Bennani   m_opaque_up->SetIsStackMemory(stack_memory ? MemoryRegionInfo::eYes
37a758c9f7SMed Ismail Bennani                                              : MemoryRegionInfo::eNo);
38a758c9f7SMed Ismail Bennani }
39a758c9f7SMed Ismail Bennani 
SBMemoryRegionInfo(const MemoryRegionInfo * lldb_object_ptr)40b9c1b51eSKate Stone SBMemoryRegionInfo::SBMemoryRegionInfo(const MemoryRegionInfo *lldb_object_ptr)
41d5b44036SJonas Devlieghere     : m_opaque_up(new MemoryRegionInfo()) {
4226036843SHoward Hellyer   if (lldb_object_ptr)
4326036843SHoward Hellyer     ref() = *lldb_object_ptr;
4426036843SHoward Hellyer }
4526036843SHoward Hellyer 
SBMemoryRegionInfo(const SBMemoryRegionInfo & rhs)46a3436f73SKazu Hirata SBMemoryRegionInfo::SBMemoryRegionInfo(const SBMemoryRegionInfo &rhs) {
471755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, rhs);
48bd4bf82aSJonas Devlieghere   m_opaque_up = clone(rhs.m_opaque_up);
4926036843SHoward Hellyer }
5026036843SHoward Hellyer 
51b9c1b51eSKate Stone const SBMemoryRegionInfo &SBMemoryRegionInfo::
operator =(const SBMemoryRegionInfo & rhs)52b9c1b51eSKate Stone operator=(const SBMemoryRegionInfo &rhs) {
531755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, rhs);
54baf5664fSJonas Devlieghere 
55bd4bf82aSJonas Devlieghere   if (this != &rhs)
56bd4bf82aSJonas Devlieghere     m_opaque_up = clone(rhs.m_opaque_up);
57d232abc3SJonas Devlieghere   return *this;
5826036843SHoward Hellyer }
5926036843SHoward Hellyer 
60866b7a65SJonas Devlieghere SBMemoryRegionInfo::~SBMemoryRegionInfo() = default;
6126036843SHoward Hellyer 
Clear()62baf5664fSJonas Devlieghere void SBMemoryRegionInfo::Clear() {
631755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
64baf5664fSJonas Devlieghere 
65baf5664fSJonas Devlieghere   m_opaque_up->Clear();
66baf5664fSJonas Devlieghere }
6726036843SHoward Hellyer 
operator ==(const SBMemoryRegionInfo & rhs) const68b9c1b51eSKate Stone bool SBMemoryRegionInfo::operator==(const SBMemoryRegionInfo &rhs) const {
691755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, rhs);
70baf5664fSJonas Devlieghere 
7126036843SHoward Hellyer   return ref() == rhs.ref();
7226036843SHoward Hellyer }
7326036843SHoward Hellyer 
operator !=(const SBMemoryRegionInfo & rhs) const74b9c1b51eSKate Stone bool SBMemoryRegionInfo::operator!=(const SBMemoryRegionInfo &rhs) const {
751755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, rhs);
76baf5664fSJonas Devlieghere 
7726036843SHoward Hellyer   return ref() != rhs.ref();
7826036843SHoward Hellyer }
7926036843SHoward Hellyer 
ref()80d5b44036SJonas Devlieghere MemoryRegionInfo &SBMemoryRegionInfo::ref() { return *m_opaque_up; }
8126036843SHoward Hellyer 
ref() const82d5b44036SJonas Devlieghere const MemoryRegionInfo &SBMemoryRegionInfo::ref() const { return *m_opaque_up; }
8326036843SHoward Hellyer 
GetRegionBase()84b9c1b51eSKate Stone lldb::addr_t SBMemoryRegionInfo::GetRegionBase() {
851755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
86baf5664fSJonas Devlieghere 
87d5b44036SJonas Devlieghere   return m_opaque_up->GetRange().GetRangeBase();
8826036843SHoward Hellyer }
8926036843SHoward Hellyer 
GetRegionEnd()90b9c1b51eSKate Stone lldb::addr_t SBMemoryRegionInfo::GetRegionEnd() {
911755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
92baf5664fSJonas Devlieghere 
93d5b44036SJonas Devlieghere   return m_opaque_up->GetRange().GetRangeEnd();
9426036843SHoward Hellyer }
9526036843SHoward Hellyer 
IsReadable()96b9c1b51eSKate Stone bool SBMemoryRegionInfo::IsReadable() {
971755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
98baf5664fSJonas Devlieghere 
99d5b44036SJonas Devlieghere   return m_opaque_up->GetReadable() == MemoryRegionInfo::eYes;
10026036843SHoward Hellyer }
10126036843SHoward Hellyer 
IsWritable()102b9c1b51eSKate Stone bool SBMemoryRegionInfo::IsWritable() {
1031755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
104baf5664fSJonas Devlieghere 
105d5b44036SJonas Devlieghere   return m_opaque_up->GetWritable() == MemoryRegionInfo::eYes;
10626036843SHoward Hellyer }
10726036843SHoward Hellyer 
IsExecutable()108b9c1b51eSKate Stone bool SBMemoryRegionInfo::IsExecutable() {
1091755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
110baf5664fSJonas Devlieghere 
111d5b44036SJonas Devlieghere   return m_opaque_up->GetExecutable() == MemoryRegionInfo::eYes;
11226036843SHoward Hellyer }
11326036843SHoward Hellyer 
IsMapped()114b9c1b51eSKate Stone bool SBMemoryRegionInfo::IsMapped() {
1151755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
116baf5664fSJonas Devlieghere 
117d5b44036SJonas Devlieghere   return m_opaque_up->GetMapped() == MemoryRegionInfo::eYes;
118ad007563SHoward Hellyer }
119ad007563SHoward Hellyer 
GetName()120b9c1b51eSKate Stone const char *SBMemoryRegionInfo::GetName() {
1211755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
122baf5664fSJonas Devlieghere 
123d5b44036SJonas Devlieghere   return m_opaque_up->GetName().AsCString();
124d7d69f80STamas Berghammer }
125d7d69f80STamas Berghammer 
HasDirtyMemoryPageList()126aa575ed9SJason Molenda bool SBMemoryRegionInfo::HasDirtyMemoryPageList() {
1271755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
1289ea6dd5cSJason Molenda 
129064a08cdSKazu Hirata   return m_opaque_up->GetDirtyPageList().has_value();
1309ea6dd5cSJason Molenda }
1319ea6dd5cSJason Molenda 
GetNumDirtyPages()132aa575ed9SJason Molenda uint32_t SBMemoryRegionInfo::GetNumDirtyPages() {
1331755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
1349ea6dd5cSJason Molenda 
1359ea6dd5cSJason Molenda   uint32_t num_dirty_pages = 0;
1367d8ec4dcSDavid Spickett   const llvm::Optional<std::vector<addr_t>> &dirty_page_list =
1379ea6dd5cSJason Molenda       m_opaque_up->GetDirtyPageList();
13896d1b4ddSKazu Hirata   if (dirty_page_list)
139*5cff5142SKazu Hirata     num_dirty_pages = dirty_page_list.value().size();
1409ea6dd5cSJason Molenda 
1419ea6dd5cSJason Molenda   return num_dirty_pages;
1429ea6dd5cSJason Molenda }
1439ea6dd5cSJason Molenda 
GetDirtyPageAddressAtIndex(uint32_t idx)144aa575ed9SJason Molenda addr_t SBMemoryRegionInfo::GetDirtyPageAddressAtIndex(uint32_t idx) {
1451755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, idx);
1469ea6dd5cSJason Molenda 
1479ea6dd5cSJason Molenda   addr_t dirty_page_addr = LLDB_INVALID_ADDRESS;
1489ea6dd5cSJason Molenda   const llvm::Optional<std::vector<addr_t>> &dirty_page_list =
1499ea6dd5cSJason Molenda       m_opaque_up->GetDirtyPageList();
150*5cff5142SKazu Hirata   if (dirty_page_list && idx < dirty_page_list.value().size())
151*5cff5142SKazu Hirata     dirty_page_addr = dirty_page_list.value()[idx];
1529ea6dd5cSJason Molenda 
1539ea6dd5cSJason Molenda   return dirty_page_addr;
1549ea6dd5cSJason Molenda }
1559ea6dd5cSJason Molenda 
GetPageSize()156aa575ed9SJason Molenda int SBMemoryRegionInfo::GetPageSize() {
1571755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
15865bc8ba1SJason Molenda 
1599ea6dd5cSJason Molenda   return m_opaque_up->GetPageSize();
1609ea6dd5cSJason Molenda }
1619ea6dd5cSJason Molenda 
GetDescription(SBStream & description)162b9c1b51eSKate Stone bool SBMemoryRegionInfo::GetDescription(SBStream &description) {
1631755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, description);
164baf5664fSJonas Devlieghere 
16526036843SHoward Hellyer   Stream &strm = description.ref();
166d5b44036SJonas Devlieghere   const addr_t load_addr = m_opaque_up->GetRange().base;
16726036843SHoward Hellyer 
168b9c1b51eSKate Stone   strm.Printf("[0x%16.16" PRIx64 "-0x%16.16" PRIx64 " ", load_addr,
169d5b44036SJonas Devlieghere               load_addr + m_opaque_up->GetRange().size);
170d5b44036SJonas Devlieghere   strm.Printf(m_opaque_up->GetReadable() ? "R" : "-");
171d5b44036SJonas Devlieghere   strm.Printf(m_opaque_up->GetWritable() ? "W" : "-");
172d5b44036SJonas Devlieghere   strm.Printf(m_opaque_up->GetExecutable() ? "X" : "-");
17326036843SHoward Hellyer   strm.Printf("]");
17426036843SHoward Hellyer 
17526036843SHoward Hellyer   return true;
17626036843SHoward Hellyer }
177