1 //===-- AddressRange.cpp ----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Core/AddressRange.h"
11 #include "lldb/Core/ArchSpec.h" // for ArchSpec
12 #include "lldb/Core/Module.h"
13 #include "lldb/Target/Target.h"
14 #include "lldb/Utility/ConstString.h" // for ConstString
15 #include "lldb/Utility/FileSpec.h" // for FileSpec
16 #include "lldb/Utility/Stream.h"
17 #include "lldb/lldb-defines.h" // for LLDB_INVALID_ADDRESS
19 #include "llvm/Support/Compiler.h" // for LLVM_FALLTHROUGH
21 #include <memory> // for shared_ptr
23 #include <inttypes.h> // for PRIx64
25 namespace lldb_private {
30 using namespace lldb_private;
32 AddressRange::AddressRange() : m_base_addr(), m_byte_size(0) {}
34 AddressRange::AddressRange(addr_t file_addr, addr_t byte_size,
35 const SectionList *section_list)
36 : m_base_addr(file_addr, section_list), m_byte_size(byte_size) {}
38 AddressRange::AddressRange(const lldb::SectionSP §ion, addr_t offset,
40 : m_base_addr(section, offset), m_byte_size(byte_size) {}
42 AddressRange::AddressRange(const Address &so_addr, addr_t byte_size)
43 : m_base_addr(so_addr), m_byte_size(byte_size) {}
45 AddressRange::~AddressRange() {}
48 // AddressRange::Contains (const Address &addr) const
50 // const addr_t byte_size = GetByteSize();
52 // return addr.GetSection() == m_base_addr.GetSection() &&
53 // (addr.GetOffset() - m_base_addr.GetOffset()) < byte_size;
57 // AddressRange::Contains (const Address *addr) const
60 // return Contains (*addr);
64 bool AddressRange::ContainsFileAddress(const Address &addr) const {
65 if (addr.GetSection() == m_base_addr.GetSection())
66 return (addr.GetOffset() - m_base_addr.GetOffset()) < GetByteSize();
67 addr_t file_base_addr = GetBaseAddress().GetFileAddress();
68 if (file_base_addr == LLDB_INVALID_ADDRESS)
71 addr_t file_addr = addr.GetFileAddress();
72 if (file_addr == LLDB_INVALID_ADDRESS)
75 if (file_base_addr <= file_addr)
76 return (file_addr - file_base_addr) < GetByteSize();
81 bool AddressRange::ContainsFileAddress(addr_t file_addr) const {
82 if (file_addr == LLDB_INVALID_ADDRESS)
85 addr_t file_base_addr = GetBaseAddress().GetFileAddress();
86 if (file_base_addr == LLDB_INVALID_ADDRESS)
89 if (file_base_addr <= file_addr)
90 return (file_addr - file_base_addr) < GetByteSize();
95 bool AddressRange::ContainsLoadAddress(const Address &addr,
96 Target *target) const {
97 if (addr.GetSection() == m_base_addr.GetSection())
98 return (addr.GetOffset() - m_base_addr.GetOffset()) < GetByteSize();
99 addr_t load_base_addr = GetBaseAddress().GetLoadAddress(target);
100 if (load_base_addr == LLDB_INVALID_ADDRESS)
103 addr_t load_addr = addr.GetLoadAddress(target);
104 if (load_addr == LLDB_INVALID_ADDRESS)
107 if (load_base_addr <= load_addr)
108 return (load_addr - load_base_addr) < GetByteSize();
113 bool AddressRange::ContainsLoadAddress(addr_t load_addr, Target *target) const {
114 if (load_addr == LLDB_INVALID_ADDRESS)
117 addr_t load_base_addr = GetBaseAddress().GetLoadAddress(target);
118 if (load_base_addr == LLDB_INVALID_ADDRESS)
121 if (load_base_addr <= load_addr)
122 return (load_addr - load_base_addr) < GetByteSize();
127 void AddressRange::Clear() {
132 bool AddressRange::Dump(Stream *s, Target *target, Address::DumpStyle style,
133 Address::DumpStyle fallback_style) const {
134 addr_t vmaddr = LLDB_INVALID_ADDRESS;
135 int addr_size = sizeof(addr_t);
137 addr_size = target->GetArchitecture().GetAddressByteSize();
139 bool show_module = false;
143 case Address::DumpStyleSectionNameOffset:
144 case Address::DumpStyleSectionPointerOffset:
146 m_base_addr.Dump(s, target, style, fallback_style);
148 s->Address(m_base_addr.GetOffset() + GetByteSize(), addr_size);
153 case Address::DumpStyleModuleWithFileAddress:
156 case Address::DumpStyleFileAddress:
157 vmaddr = m_base_addr.GetFileAddress();
160 case Address::DumpStyleLoadAddress:
161 vmaddr = m_base_addr.GetLoadAddress(target);
165 if (vmaddr != LLDB_INVALID_ADDRESS) {
167 ModuleSP module_sp(GetBaseAddress().GetModule());
169 s->Printf("%s", module_sp->GetFileSpec().GetFilename().AsCString(
172 s->AddressRange(vmaddr, vmaddr + GetByteSize(), addr_size);
174 } else if (fallback_style != Address::DumpStyleInvalid) {
175 return Dump(s, target, fallback_style, Address::DumpStyleInvalid);
181 void AddressRange::DumpDebug(Stream *s) const {
182 s->Printf("%p: AddressRange section = %p, offset = 0x%16.16" PRIx64
183 ", byte_size = 0x%16.16" PRIx64 "\n",
184 static_cast<const void *>(this),
185 static_cast<void *>(m_base_addr.GetSection().get()),
186 m_base_addr.GetOffset(), GetByteSize());
190 // lldb::operator== (const AddressRange& lhs, const AddressRange& rhs)
192 // if (lhs.GetBaseAddress() == rhs.GetBaseAddress())
193 // return lhs.GetByteSize() == rhs.GetByteSize();