1 //===-- MemoryRegionInfo.h ---------------------------------------*- C++
2 //-*-===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10 
11 #ifndef lldb_MemoryRegionInfo_h
12 #define lldb_MemoryRegionInfo_h
13 
14 #include "lldb/Core/RangeMap.h"
15 #include "llvm/Support/FormatProviders.h"
16 #include "lldb/Utility/ConstString.h"
17 
18 namespace lldb_private {
19 class MemoryRegionInfo {
20 public:
21   typedef Range<lldb::addr_t, lldb::addr_t> RangeType;
22 
23   enum OptionalBool { eDontKnow = -1, eNo = 0, eYes = 1 };
24 
MemoryRegionInfo()25   MemoryRegionInfo()
26       : m_range(), m_read(eDontKnow), m_write(eDontKnow), m_execute(eDontKnow),
27         m_mapped(eDontKnow), m_flash(eDontKnow), m_blocksize(0) {}
28 
~MemoryRegionInfo()29   ~MemoryRegionInfo() {}
30 
GetRange()31   RangeType &GetRange() { return m_range; }
32 
Clear()33   void Clear() {
34     m_range.Clear();
35     m_read = m_write = m_execute = eDontKnow;
36   }
37 
GetRange()38   const RangeType &GetRange() const { return m_range; }
39 
GetReadable()40   OptionalBool GetReadable() const { return m_read; }
41 
GetWritable()42   OptionalBool GetWritable() const { return m_write; }
43 
GetExecutable()44   OptionalBool GetExecutable() const { return m_execute; }
45 
GetMapped()46   OptionalBool GetMapped() const { return m_mapped; }
47 
GetName()48   const ConstString &GetName() const { return m_name; }
49 
SetReadable(OptionalBool val)50   void SetReadable(OptionalBool val) { m_read = val; }
51 
SetWritable(OptionalBool val)52   void SetWritable(OptionalBool val) { m_write = val; }
53 
SetExecutable(OptionalBool val)54   void SetExecutable(OptionalBool val) { m_execute = val; }
55 
SetMapped(OptionalBool val)56   void SetMapped(OptionalBool val) { m_mapped = val; }
57 
SetName(const char * name)58   void SetName(const char *name) { m_name = ConstString(name); }
59 
GetFlash()60   OptionalBool GetFlash() const { return m_flash; }
61 
SetFlash(OptionalBool val)62   void SetFlash(OptionalBool val) { m_flash = val; }
63 
GetBlocksize()64   lldb::offset_t GetBlocksize() const { return m_blocksize; }
65 
SetBlocksize(lldb::offset_t blocksize)66   void SetBlocksize(lldb::offset_t blocksize) { m_blocksize = blocksize; }
67 
68   //----------------------------------------------------------------------
69   // Get permissions as a uint32_t that is a mask of one or more bits from the
70   // lldb::Permissions
71   //----------------------------------------------------------------------
GetLLDBPermissions()72   uint32_t GetLLDBPermissions() const {
73     uint32_t permissions = 0;
74     if (m_read)
75       permissions |= lldb::ePermissionsReadable;
76     if (m_write)
77       permissions |= lldb::ePermissionsWritable;
78     if (m_execute)
79       permissions |= lldb::ePermissionsExecutable;
80     return permissions;
81   }
82 
83   //----------------------------------------------------------------------
84   // Set permissions from a uint32_t that contains one or more bits from the
85   // lldb::Permissions
86   //----------------------------------------------------------------------
SetLLDBPermissions(uint32_t permissions)87   void SetLLDBPermissions(uint32_t permissions) {
88     m_read = (permissions & lldb::ePermissionsReadable) ? eYes : eNo;
89     m_write = (permissions & lldb::ePermissionsWritable) ? eYes : eNo;
90     m_execute = (permissions & lldb::ePermissionsExecutable) ? eYes : eNo;
91   }
92 
93   bool operator==(const MemoryRegionInfo &rhs) const {
94     return m_range == rhs.m_range && m_read == rhs.m_read &&
95            m_write == rhs.m_write && m_execute == rhs.m_execute &&
96            m_mapped == rhs.m_mapped;
97   }
98 
99   bool operator!=(const MemoryRegionInfo &rhs) const { return !(*this == rhs); }
100 
101 protected:
102   RangeType m_range;
103   OptionalBool m_read;
104   OptionalBool m_write;
105   OptionalBool m_execute;
106   OptionalBool m_mapped;
107   ConstString m_name;
108   OptionalBool m_flash;
109   lldb::offset_t m_blocksize;
110 };
111 
112 inline bool operator<(const MemoryRegionInfo &lhs,
113                       const MemoryRegionInfo &rhs) {
114   return lhs.GetRange() < rhs.GetRange();
115 }
116 
117 inline bool operator<(const MemoryRegionInfo &lhs, lldb::addr_t rhs) {
118   return lhs.GetRange().GetRangeBase() < rhs;
119 }
120 
121 inline bool operator<(lldb::addr_t lhs, const MemoryRegionInfo &rhs) {
122   return lhs < rhs.GetRange().GetRangeBase();
123 }
124 
125 // Forward-declarable wrapper.
126 class MemoryRegionInfos : public std::vector<lldb_private::MemoryRegionInfo> {
127 public:
128   //using std::vector<lldb_private::MemoryRegionInfo>::vector;
129 };
130 
131 }
132 
133 namespace llvm {
134 template <>
135 struct format_provider<lldb_private::MemoryRegionInfo::OptionalBool> {
136   static void format(const lldb_private::MemoryRegionInfo::OptionalBool &B,
137                      raw_ostream &OS, StringRef Options) {
138     switch(B) {
139     case lldb_private::MemoryRegionInfo::eNo:
140       OS << "no";
141       return;
142     case lldb_private::MemoryRegionInfo::eYes:
143       OS << "yes";
144       return;
145     case lldb_private::MemoryRegionInfo::eDontKnow:
146       OS << "don't know";
147       return;
148     }
149   }
150 };
151 }
152 
153 #endif // #ifndef lldb_MemoryRegionInfo_h
154