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_MemoryRegionInfo_h
11 #define lldb_MemoryRegionInfo_h
12 
13 #include "lldb/Utility/ConstString.h"
14 #include "lldb/Utility/RangeMap.h"
15 #include "llvm/Support/FormatProviders.h"
16 
17 namespace lldb_private {
18 class MemoryRegionInfo {
19 public:
20   typedef Range<lldb::addr_t, lldb::addr_t> RangeType;
21 
22   enum OptionalBool { eDontKnow = -1, eNo = 0, eYes = 1 };
23 
24   MemoryRegionInfo() = default;
25   MemoryRegionInfo(RangeType range, OptionalBool read, OptionalBool write,
26                    OptionalBool execute, OptionalBool mapped, ConstString name,
27                    OptionalBool flash, lldb::offset_t blocksize)
28       : m_range(range), m_read(read), m_write(write), m_execute(execute),
29         m_mapped(mapped), m_name(name), m_flash(flash), m_blocksize(blocksize) {
30   }
31 
32   RangeType &GetRange() { return m_range; }
33 
34   void Clear() {
35     m_range.Clear();
36     m_read = m_write = m_execute = eDontKnow;
37   }
38 
39   const RangeType &GetRange() const { return m_range; }
40 
41   OptionalBool GetReadable() const { return m_read; }
42 
43   OptionalBool GetWritable() const { return m_write; }
44 
45   OptionalBool GetExecutable() const { return m_execute; }
46 
47   OptionalBool GetMapped() const { return m_mapped; }
48 
49   ConstString GetName() const { return m_name; }
50 
51   void SetReadable(OptionalBool val) { m_read = val; }
52 
53   void SetWritable(OptionalBool val) { m_write = val; }
54 
55   void SetExecutable(OptionalBool val) { m_execute = val; }
56 
57   void SetMapped(OptionalBool val) { m_mapped = val; }
58 
59   void SetName(const char *name) { m_name = ConstString(name); }
60 
61   OptionalBool GetFlash() const { return m_flash; }
62 
63   void SetFlash(OptionalBool val) { m_flash = val; }
64 
65   lldb::offset_t GetBlocksize() const { return m_blocksize; }
66 
67   void SetBlocksize(lldb::offset_t blocksize) { m_blocksize = blocksize; }
68 
69   // Get permissions as a uint32_t that is a mask of one or more bits from the
70   // lldb::Permissions
71   uint32_t GetLLDBPermissions() const {
72     uint32_t permissions = 0;
73     if (m_read)
74       permissions |= lldb::ePermissionsReadable;
75     if (m_write)
76       permissions |= lldb::ePermissionsWritable;
77     if (m_execute)
78       permissions |= lldb::ePermissionsExecutable;
79     return permissions;
80   }
81 
82   // Set permissions from a uint32_t that contains one or more bits from the
83   // lldb::Permissions
84   void SetLLDBPermissions(uint32_t permissions) {
85     m_read = (permissions & lldb::ePermissionsReadable) ? eYes : eNo;
86     m_write = (permissions & lldb::ePermissionsWritable) ? eYes : eNo;
87     m_execute = (permissions & lldb::ePermissionsExecutable) ? eYes : eNo;
88   }
89 
90   bool operator==(const MemoryRegionInfo &rhs) const {
91     return m_range == rhs.m_range && m_read == rhs.m_read &&
92            m_write == rhs.m_write && m_execute == rhs.m_execute &&
93            m_mapped == rhs.m_mapped && m_name == rhs.m_name &&
94            m_flash == rhs.m_flash && m_blocksize == rhs.m_blocksize;
95   }
96 
97   bool operator!=(const MemoryRegionInfo &rhs) const { return !(*this == rhs); }
98 
99 protected:
100   RangeType m_range;
101   OptionalBool m_read = eDontKnow;
102   OptionalBool m_write = eDontKnow;
103   OptionalBool m_execute = eDontKnow;
104   OptionalBool m_mapped = eDontKnow;
105   ConstString m_name;
106   OptionalBool m_flash = eDontKnow;
107   lldb::offset_t m_blocksize = 0;
108 };
109 
110 inline bool operator<(const MemoryRegionInfo &lhs,
111                       const MemoryRegionInfo &rhs) {
112   return lhs.GetRange() < rhs.GetRange();
113 }
114 
115 inline bool operator<(const MemoryRegionInfo &lhs, lldb::addr_t rhs) {
116   return lhs.GetRange().GetRangeBase() < rhs;
117 }
118 
119 inline bool operator<(lldb::addr_t lhs, const MemoryRegionInfo &rhs) {
120   return lhs < rhs.GetRange().GetRangeBase();
121 }
122 
123 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
124                               const MemoryRegionInfo &Info);
125 
126 // Forward-declarable wrapper.
127 class MemoryRegionInfos : public std::vector<lldb_private::MemoryRegionInfo> {
128 public:
129   //using std::vector<lldb_private::MemoryRegionInfo>::vector;
130 };
131 
132 }
133 
134 namespace llvm {
135 template <>
136 /// If Options is empty, prints a textual representation of the value. If
137 /// Options is a single character, it uses that character for the "yes" value,
138 /// while "no" is printed as "-", and "don't know" as "?". This can be used to
139 /// print the permissions in the traditional "rwx" form.
140 struct format_provider<lldb_private::MemoryRegionInfo::OptionalBool> {
141   static void format(const lldb_private::MemoryRegionInfo::OptionalBool &B,
142                      raw_ostream &OS, StringRef Options);
143 };
144 }
145 
146 #endif // #ifndef lldb_MemoryRegionInfo_h
147