xref: /freebsd/contrib/llvm-project/lldb/include/lldb/Core/AddressRange.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1 //===-- AddressRange.h ------------------------------------------*- C++ -*-===//
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 #ifndef LLDB_CORE_ADDRESSRANGE_H
10 #define LLDB_CORE_ADDRESSRANGE_H
11 
12 #include "lldb/Core/Address.h"
13 #include "lldb/lldb-forward.h"
14 #include "lldb/lldb-types.h"
15 
16 #include <cstddef>
17 
18 namespace lldb_private {
19 class SectionList;
20 class Stream;
21 class Target;
22 
23 /// \class AddressRange AddressRange.h "lldb/Core/AddressRange.h"
24 /// A section + offset based address range class.
25 class AddressRange {
26 public:
27   /// Default constructor.
28   ///
29   /// Initialize with a invalid section (NULL), an invalid offset
30   /// (LLDB_INVALID_ADDRESS), and zero byte size.
31   AddressRange();
32 
33   /// Construct with a section pointer, offset, and byte_size.
34   ///
35   /// Initialize the address with the supplied \a section, \a offset and \a
36   /// byte_size.
37   ///
38   /// \param[in] section
39   ///     A section pointer to a valid lldb::Section, or NULL if the
40   ///     address doesn't have a section or will get resolved later.
41   ///
42   /// \param[in] offset
43   ///     The offset in bytes into \a section.
44   ///
45   /// \param[in] byte_size
46   ///     The size in bytes of the address range.
47   AddressRange(const lldb::SectionSP &section, lldb::addr_t offset,
48                lldb::addr_t byte_size);
49 
50   /// Construct with a virtual address, section list and byte size.
51   ///
52   /// Initialize and resolve the address with the supplied virtual address \a
53   /// file_addr, and byte size \a byte_size.
54   ///
55   /// \param[in] file_addr
56   ///     A virtual address.
57   ///
58   /// \param[in] byte_size
59   ///     The size in bytes of the address range.
60   ///
61   /// \param[in] section_list
62   ///     A list of sections, one of which may contain the \a vaddr.
63   AddressRange(lldb::addr_t file_addr, lldb::addr_t byte_size,
64                const SectionList *section_list = nullptr);
65 
66   /// Construct with a Address object address and byte size.
67   ///
68   /// Initialize by copying the section offset address in \a so_addr, and
69   /// setting the byte size to \a byte_size.
70   ///
71   /// \param[in] so_addr
72   ///     A section offset address object.
73   ///
74   /// \param[in] byte_size
75   ///     The size in bytes of the address range.
76   AddressRange(const Address &so_addr, lldb::addr_t byte_size);
77 
78   /// Destructor.
79   ///
80   /// The destructor is virtual in case this class is subclassed.
81   ~AddressRange();
82 
83   /// Clear the object's state.
84   ///
85   /// Sets the section to an invalid value (NULL), an invalid offset
86   /// (LLDB_INVALID_ADDRESS) and a zero byte size.
87   void Clear();
88 
89   bool IsValid() const;
90 
91   /// Check if a section offset address is contained in this range.
92   ///
93   /// \param[in] so_addr
94   ///     A section offset address object reference.
95   ///
96   /// \return
97   ///     Returns \b true if \a so_addr is contained in this range,
98   ///     \b false otherwise.
99   bool Contains(const Address &so_addr) const;
100 
101   /// Check if a section offset address is contained in this range.
102   ///
103   /// \param[in] so_addr_ptr
104   ///     A section offset address object pointer.
105   ///
106   /// \return
107   ///     Returns \b true if \a so_addr is contained in this range,
108   ///     \b false otherwise.
109   //    bool
110   //    Contains (const Address *so_addr_ptr) const;
111 
112   /// Check if a section offset \a so_addr when represented as a file address
113   /// is contained within this object's file address range.
114   ///
115   /// \param[in] so_addr
116   ///     A section offset address object reference.
117   ///
118   /// \return
119   ///     Returns \b true if both \a this and \a so_addr have
120   ///     resolvable file address values and \a so_addr is contained
121   ///     in the address range, \b false otherwise.
122   bool ContainsFileAddress(const Address &so_addr) const;
123 
124   /// Check if the resolved file address \a file_addr is contained within this
125   /// object's file address range.
126   ///
127   /// \param[in] file_addr
128   ///     A section offset address object reference.
129   ///
130   /// \return
131   ///     Returns \b true if both \a this has a resolvable file
132   ///     address value and \a so_addr is contained in the address
133   ///     range, \b false otherwise.
134   bool ContainsFileAddress(lldb::addr_t file_addr) const;
135 
136   /// Check if a section offset \a so_addr when represented as a load address
137   /// is contained within this object's load address range.
138   ///
139   /// \param[in] so_addr
140   ///     A section offset address object reference.
141   ///
142   /// \return
143   ///     Returns \b true if both \a this and \a so_addr have
144   ///     resolvable load address values and \a so_addr is contained
145   ///     in the address range, \b false otherwise.
146   bool ContainsLoadAddress(const Address &so_addr, Target *target) const;
147 
148   /// Check if the resolved load address \a load_addr is contained within this
149   /// object's load address range.
150   ///
151   /// \return
152   ///     Returns \b true if both \a this has a resolvable load
153   ///     address value and \a so_addr is contained in the address
154   ///     range, \b false otherwise.
155   bool ContainsLoadAddress(lldb::addr_t load_addr, Target *target) const;
156 
157   //------------------------------------------------------------------
158   /// Extends this range with \b rhs_range if it overlaps this range on the
159   /// right side. The range overlaps on the right side if the base address
160   /// of \b rhs_range lies within this range or if it's contiguous on its
161   /// right side.
162   ///
163   /// @param[in] rhs_range
164   ///     The range to extend at the right side.
165   ///
166   /// @return
167   ///     Returns \b true if this range was extended, \b false otherwise.
168   //------------------------------------------------------------------
169   bool Extend(const AddressRange &rhs_range);
170 
171   /// Dump a description of this object to a Stream.
172   ///
173   /// Dump a description of the contents of this object to the supplied stream
174   /// \a s. There are many ways to display a section offset based address
175   /// range, and \a style lets the user choose how the base address gets
176   /// displayed.
177   ///
178   /// \param[in] s
179   ///     The stream to which to dump the object description.
180   ///
181   /// \param[in] style
182   ///     The display style for the address.
183   ///
184   /// \return
185   ///     Returns \b true if the address was able to be displayed.
186   ///     File and load addresses may be unresolved and it may not be
187   ///     possible to display a valid value, \b false will be returned
188   ///     in such cases.
189   ///
190   /// \see Address::DumpStyle
191   bool
192   Dump(Stream *s, Target *target, Address::DumpStyle style,
193        Address::DumpStyle fallback_style = Address::DumpStyleInvalid) const;
194 
195   /// Dump a debug description of this object to a Stream.
196   ///
197   /// Dump a debug description of the contents of this object to the supplied
198   /// stream \a s.
199   ///
200   /// The debug description contains verbose internal state such and pointer
201   /// values, reference counts, etc.
202   ///
203   /// \param[in] s
204   ///     The stream to which to dump the object description.
205   void DumpDebug(Stream *s) const;
206 
207   /// Get accessor for the base address of the range.
208   ///
209   /// \return
210   ///     A reference to the base address object.
GetBaseAddress()211   Address &GetBaseAddress() { return m_base_addr; }
212 
213   /// Get const accessor for the base address of the range.
214   ///
215   /// \return
216   ///     A const reference to the base address object.
GetBaseAddress()217   const Address &GetBaseAddress() const { return m_base_addr; }
218 
219   /// Get accessor for the byte size of this range.
220   ///
221   /// \return
222   ///     The size in bytes of this address range.
GetByteSize()223   lldb::addr_t GetByteSize() const { return m_byte_size; }
224 
225   /// Get the memory cost of this object.
226   ///
227   /// \return
228   ///     The number of bytes that this object occupies in memory.
MemorySize()229   size_t MemorySize() const {
230     // Noting special for the memory size of a single AddressRange object, it
231     // is just the size of itself.
232     return sizeof(AddressRange);
233   }
234 
235   /// Set accessor for the byte size of this range.
236   ///
237   /// \param[in] byte_size
238   ///     The new size in bytes of this address range.
SetByteSize(lldb::addr_t byte_size)239   void SetByteSize(lldb::addr_t byte_size) { m_byte_size = byte_size; }
240 
241   bool GetDescription(Stream *s, Target *target) const;
242 
243   bool operator==(const AddressRange &rhs);
244 
245   bool operator!=(const AddressRange &rhs);
246 
247 protected:
248   // Member variables
249   Address m_base_addr;      ///< The section offset base address of this range.
250   lldb::addr_t m_byte_size = 0; ///< The size in bytes of this address range.
251 };
252 
253 // Forward-declarable wrapper.
254 class AddressRanges : public std::vector<lldb_private::AddressRange> {
255 public:
256   using std::vector<lldb_private::AddressRange>::vector;
257 };
258 
259 } // namespace lldb_private
260 
261 #endif // LLDB_CORE_ADDRESSRANGE_H
262