1 //===-- MemoryRegionInfo.h ---------------------------------------*- C++
2 //-*-===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLDB_TARGET_MEMORYREGIONINFO_H
11 #define LLDB_TARGET_MEMORYREGIONINFO_H
12 
13 #include <vector>
14 
15 #include "lldb/Utility/ConstString.h"
16 #include "lldb/Utility/RangeMap.h"
17 #include "llvm/ADT/Optional.h"
18 #include "llvm/Support/FormatProviders.h"
19 
20 namespace lldb_private {
21 class MemoryRegionInfo {
22 public:
23   typedef Range<lldb::addr_t, lldb::addr_t> RangeType;
24 
25   enum OptionalBool { eDontKnow = -1, eNo = 0, eYes = 1 };
26 
27   MemoryRegionInfo() = default;
MemoryRegionInfo(RangeType range,OptionalBool read,OptionalBool write,OptionalBool execute,OptionalBool mapped,ConstString name,OptionalBool flash,lldb::offset_t blocksize,OptionalBool memory_tagged)28   MemoryRegionInfo(RangeType range, OptionalBool read, OptionalBool write,
29                    OptionalBool execute, OptionalBool mapped, ConstString name,
30                    OptionalBool flash, lldb::offset_t blocksize,
31                    OptionalBool memory_tagged)
32       : m_range(range), m_read(read), m_write(write), m_execute(execute),
33         m_mapped(mapped), m_name(name), m_flash(flash), m_blocksize(blocksize),
34         m_memory_tagged(memory_tagged) {}
35 
GetRange()36   RangeType &GetRange() { return m_range; }
37 
Clear()38   void Clear() { *this = MemoryRegionInfo(); }
39 
GetRange()40   const RangeType &GetRange() const { return m_range; }
41 
GetReadable()42   OptionalBool GetReadable() const { return m_read; }
43 
GetWritable()44   OptionalBool GetWritable() const { return m_write; }
45 
GetExecutable()46   OptionalBool GetExecutable() const { return m_execute; }
47 
GetMapped()48   OptionalBool GetMapped() const { return m_mapped; }
49 
GetName()50   ConstString GetName() const { return m_name; }
51 
GetMemoryTagged()52   OptionalBool GetMemoryTagged() const { return m_memory_tagged; }
53 
SetReadable(OptionalBool val)54   void SetReadable(OptionalBool val) { m_read = val; }
55 
SetWritable(OptionalBool val)56   void SetWritable(OptionalBool val) { m_write = val; }
57 
SetExecutable(OptionalBool val)58   void SetExecutable(OptionalBool val) { m_execute = val; }
59 
SetMapped(OptionalBool val)60   void SetMapped(OptionalBool val) { m_mapped = val; }
61 
SetName(const char * name)62   void SetName(const char *name) { m_name = ConstString(name); }
63 
GetFlash()64   OptionalBool GetFlash() const { return m_flash; }
65 
SetFlash(OptionalBool val)66   void SetFlash(OptionalBool val) { m_flash = val; }
67 
GetBlocksize()68   lldb::offset_t GetBlocksize() const { return m_blocksize; }
69 
SetBlocksize(lldb::offset_t blocksize)70   void SetBlocksize(lldb::offset_t blocksize) { m_blocksize = blocksize; }
71 
SetMemoryTagged(OptionalBool val)72   void SetMemoryTagged(OptionalBool val) { m_memory_tagged = val; }
73 
74   // Get permissions as a uint32_t that is a mask of one or more bits from the
75   // lldb::Permissions
GetLLDBPermissions()76   uint32_t GetLLDBPermissions() const {
77     uint32_t permissions = 0;
78     if (m_read)
79       permissions |= lldb::ePermissionsReadable;
80     if (m_write)
81       permissions |= lldb::ePermissionsWritable;
82     if (m_execute)
83       permissions |= lldb::ePermissionsExecutable;
84     return permissions;
85   }
86 
87   // Set permissions from a uint32_t that contains one or more bits from the
88   // lldb::Permissions
SetLLDBPermissions(uint32_t permissions)89   void SetLLDBPermissions(uint32_t permissions) {
90     m_read = (permissions & lldb::ePermissionsReadable) ? eYes : eNo;
91     m_write = (permissions & lldb::ePermissionsWritable) ? eYes : eNo;
92     m_execute = (permissions & lldb::ePermissionsExecutable) ? eYes : eNo;
93   }
94 
95   bool operator==(const MemoryRegionInfo &rhs) const {
96     return m_range == rhs.m_range && m_read == rhs.m_read &&
97            m_write == rhs.m_write && m_execute == rhs.m_execute &&
98            m_mapped == rhs.m_mapped && m_name == rhs.m_name &&
99            m_flash == rhs.m_flash && m_blocksize == rhs.m_blocksize &&
100            m_memory_tagged == rhs.m_memory_tagged &&
101            m_pagesize == rhs.m_pagesize;
102   }
103 
104   bool operator!=(const MemoryRegionInfo &rhs) const { return !(*this == rhs); }
105 
106   /// Get the target system's VM page size in bytes.
107   /// \return
108   ///     0 is returned if this information is unavailable.
GetPageSize()109   int GetPageSize() { return m_pagesize; }
110 
111   /// Get a vector of target VM pages that are dirty -- that have been
112   /// modified -- within this memory region.  This is an Optional return
113   /// value; it will only be available if the remote stub was able to
114   /// detail this.
GetDirtyPageList()115   llvm::Optional<std::vector<lldb::addr_t>> &GetDirtyPageList() {
116     return m_dirty_pages;
117   }
118 
SetPageSize(int pagesize)119   void SetPageSize(int pagesize) { m_pagesize = pagesize; }
120 
SetDirtyPageList(std::vector<lldb::addr_t> pagelist)121   void SetDirtyPageList(std::vector<lldb::addr_t> pagelist) {
122     if (m_dirty_pages.hasValue())
123       m_dirty_pages.getValue().clear();
124     m_dirty_pages = std::move(pagelist);
125   }
126 
127 protected:
128   RangeType m_range;
129   OptionalBool m_read = eDontKnow;
130   OptionalBool m_write = eDontKnow;
131   OptionalBool m_execute = eDontKnow;
132   OptionalBool m_mapped = eDontKnow;
133   ConstString m_name;
134   OptionalBool m_flash = eDontKnow;
135   lldb::offset_t m_blocksize = 0;
136   OptionalBool m_memory_tagged = eDontKnow;
137   int m_pagesize = 0;
138   llvm::Optional<std::vector<lldb::addr_t>> m_dirty_pages;
139 };
140 
141 inline bool operator<(const MemoryRegionInfo &lhs,
142                       const MemoryRegionInfo &rhs) {
143   return lhs.GetRange() < rhs.GetRange();
144 }
145 
146 inline bool operator<(const MemoryRegionInfo &lhs, lldb::addr_t rhs) {
147   return lhs.GetRange().GetRangeBase() < rhs;
148 }
149 
150 inline bool operator<(lldb::addr_t lhs, const MemoryRegionInfo &rhs) {
151   return lhs < rhs.GetRange().GetRangeBase();
152 }
153 
154 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
155                               const MemoryRegionInfo &Info);
156 
157 // Forward-declarable wrapper.
158 class MemoryRegionInfos : public std::vector<lldb_private::MemoryRegionInfo> {
159 public:
160   //using std::vector<lldb_private::MemoryRegionInfo>::vector;
161 };
162 
163 }
164 
165 namespace llvm {
166 template <>
167 /// If Options is empty, prints a textual representation of the value. If
168 /// Options is a single character, it uses that character for the "yes" value,
169 /// while "no" is printed as "-", and "don't know" as "?". This can be used to
170 /// print the permissions in the traditional "rwx" form.
171 struct format_provider<lldb_private::MemoryRegionInfo::OptionalBool> {
172   static void format(const lldb_private::MemoryRegionInfo::OptionalBool &B,
173                      raw_ostream &OS, StringRef Options);
174 };
175 }
176 
177 #endif // LLDB_TARGET_MEMORYREGIONINFO_H
178