LLDB mainline
AddressRange.cpp
Go to the documentation of this file.
1//===-- AddressRange.cpp --------------------------------------------------===//
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
10#include "lldb/Core/Module.h"
11#include "lldb/Core/Section.h"
12#include "lldb/Target/Target.h"
15#include "lldb/Utility/Stream.h"
16#include "lldb/lldb-defines.h"
17
18#include "llvm/Support/Compiler.h"
19
20#include <memory>
21
22#include <cinttypes>
23
24namespace lldb_private {
25class SectionList;
26}
27
28using namespace lldb;
29using namespace lldb_private;
30
31AddressRange::AddressRange() : m_base_addr() {}
32
34 const SectionList *section_list)
35 : m_base_addr(file_addr, section_list), m_byte_size(byte_size) {}
36
38 addr_t byte_size)
39 : m_base_addr(section, offset), m_byte_size(byte_size) {}
40
41AddressRange::AddressRange(const Address &so_addr, addr_t byte_size)
42 : m_base_addr(so_addr), m_byte_size(byte_size) {}
43
45
46bool AddressRange::Contains(const Address &addr) const {
47 SectionSP range_sect_sp = GetBaseAddress().GetSection();
48 SectionSP addr_sect_sp = addr.GetSection();
49 if (range_sect_sp) {
50 if (!addr_sect_sp ||
51 range_sect_sp->GetModule() != addr_sect_sp->GetModule())
52 return false; // Modules do not match.
53 } else if (addr_sect_sp) {
54 return false; // Range has no module but "addr" does because addr has a
55 // section
56 }
57 // Either the modules match, or both have no module, so it is ok to compare
58 // the file addresses in this case only.
59 return ContainsFileAddress(addr);
60}
61
63 if (addr.GetSection() == m_base_addr.GetSection())
64 return (addr.GetOffset() - m_base_addr.GetOffset()) < GetByteSize();
65 addr_t file_base_addr = GetBaseAddress().GetFileAddress();
66 if (file_base_addr == LLDB_INVALID_ADDRESS)
67 return false;
68
69 addr_t file_addr = addr.GetFileAddress();
70 if (file_addr == LLDB_INVALID_ADDRESS)
71 return false;
72
73 if (file_base_addr <= file_addr)
74 return (file_addr - file_base_addr) < GetByteSize();
75
76 return false;
77}
78
80 if (file_addr == LLDB_INVALID_ADDRESS)
81 return false;
82
83 addr_t file_base_addr = GetBaseAddress().GetFileAddress();
84 if (file_base_addr == LLDB_INVALID_ADDRESS)
85 return false;
86
87 if (file_base_addr <= file_addr)
88 return (file_addr - file_base_addr) < GetByteSize();
89
90 return false;
91}
92
94 Target *target) const {
95 if (addr.GetSection() == m_base_addr.GetSection())
96 return (addr.GetOffset() - m_base_addr.GetOffset()) < GetByteSize();
97 addr_t load_base_addr = GetBaseAddress().GetLoadAddress(target);
98 if (load_base_addr == LLDB_INVALID_ADDRESS)
99 return false;
100
101 addr_t load_addr = addr.GetLoadAddress(target);
102 if (load_addr == LLDB_INVALID_ADDRESS)
103 return false;
104
105 if (load_base_addr <= load_addr)
106 return (load_addr - load_base_addr) < GetByteSize();
107
108 return false;
109}
110
111bool AddressRange::ContainsLoadAddress(addr_t load_addr, Target *target) const {
112 if (load_addr == LLDB_INVALID_ADDRESS)
113 return false;
114
115 addr_t load_base_addr = GetBaseAddress().GetLoadAddress(target);
116 if (load_base_addr == LLDB_INVALID_ADDRESS)
117 return false;
118
119 if (load_base_addr <= load_addr)
120 return (load_addr - load_base_addr) < GetByteSize();
121
122 return false;
123}
124
125bool AddressRange::Extend(const AddressRange &rhs_range) {
126 addr_t lhs_end_addr = GetBaseAddress().GetFileAddress() + GetByteSize();
127 addr_t rhs_base_addr = rhs_range.GetBaseAddress().GetFileAddress();
128
129 if (!ContainsFileAddress(rhs_range.GetBaseAddress()) &&
130 lhs_end_addr != rhs_base_addr)
131 // The ranges don't intersect at all on the right side of this range.
132 return false;
133
134 addr_t rhs_end_addr = rhs_base_addr + rhs_range.GetByteSize();
135 if (lhs_end_addr >= rhs_end_addr)
136 // The rhs range totally overlaps this one, nothing to add.
137 return false;
138
139 m_byte_size += rhs_end_addr - lhs_end_addr;
140 return true;
141}
142
145 m_byte_size = 0;
146}
147
149 Address::DumpStyle fallback_style) const {
151 int addr_size = sizeof(addr_t);
152 if (target)
153 addr_size = target->GetArchitecture().GetAddressByteSize();
154
155 bool show_module = false;
156 switch (style) {
157 default:
158 break;
161 s->PutChar('[');
162 m_base_addr.Dump(s, target, style, fallback_style);
163 s->PutChar('-');
165 addr_size);
166 s->PutChar(')');
167 return true;
168 break;
169
171 show_module = true;
172 [[fallthrough]];
174 vmaddr = m_base_addr.GetFileAddress();
175 break;
176
178 vmaddr = m_base_addr.GetLoadAddress(target);
179 break;
180 }
181
182 if (vmaddr != LLDB_INVALID_ADDRESS) {
183 if (show_module) {
184 ModuleSP module_sp(GetBaseAddress().GetModule());
185 if (module_sp)
186 s->Printf("%s", module_sp->GetFileSpec().GetFilename().AsCString(
187 "<Unknown>"));
188 }
189 DumpAddressRange(s->AsRawOstream(), vmaddr, vmaddr + GetByteSize(),
190 addr_size);
191 return true;
192 } else if (fallback_style != Address::DumpStyleInvalid) {
193 return Dump(s, target, fallback_style, Address::DumpStyleInvalid);
194 }
195
196 return false;
197}
198
200 s->Printf("%p: AddressRange section = %p, offset = 0x%16.16" PRIx64
201 ", byte_size = 0x%16.16" PRIx64 "\n",
202 static_cast<const void *>(this),
203 static_cast<void *>(m_base_addr.GetSection().get()),
205}
A section + offset based address range class.
Definition: AddressRange.h:25
Address & GetBaseAddress()
Get accessor for the base address of the range.
Definition: AddressRange.h:209
bool ContainsFileAddress(const Address &so_addr) const
Check if a section offset address is contained in this range.
Address m_base_addr
The section offset base address of this range.
Definition: AddressRange.h:241
bool Dump(Stream *s, Target *target, Address::DumpStyle style, Address::DumpStyle fallback_style=Address::DumpStyleInvalid) const
Dump a description of this object to a Stream.
void Clear()
Clear the object's state.
void DumpDebug(Stream *s) const
Dump a debug description of this object to a Stream.
AddressRange()
Default constructor.
bool Contains(const Address &so_addr) const
Check if a section offset address is contained in this range.
bool ContainsLoadAddress(const Address &so_addr, Target *target) const
Check if a section offset so_addr when represented as a load address is contained within this object'...
bool Extend(const AddressRange &rhs_range)
Extends this range with rhs_range if it overlaps this range on the right side.
lldb::addr_t GetByteSize() const
Get accessor for the byte size of this range.
Definition: AddressRange.h:221
lldb::addr_t m_byte_size
The size in bytes of this address range.
Definition: AddressRange.h:242
A section + offset based address class.
Definition: Address.h:62
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
Definition: Address.cpp:313
lldb::SectionSP GetSection() const
Get const accessor for the section.
Definition: Address.h:439
void Clear()
Clear the object's state.
Definition: Address.h:181
DumpStyle
Dump styles allow the Address::Dump(Stream *,DumpStyle) const function to display Address contents in...
Definition: Address.h:66
@ DumpStyleFileAddress
Display as the file address (if any).
Definition: Address.h:87
@ DumpStyleSectionNameOffset
Display as the section name + offset.
Definition: Address.h:74
@ DumpStyleInvalid
Invalid dump style.
Definition: Address.h:68
@ DumpStyleModuleWithFileAddress
Display as the file address with the module name prepended (if any).
Definition: Address.h:93
@ DumpStyleSectionPointerOffset
Display as the section pointer + offset (debug output).
Definition: Address.h:80
@ DumpStyleLoadAddress
Display as the load address (if resolved).
Definition: Address.h:99
bool Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style, DumpStyle fallback_style=DumpStyleInvalid, uint32_t addr_byte_size=UINT32_MAX, bool all_ranges=false, std::optional< Stream::HighlightSettings > settings=std::nullopt) const
Dump a description of this object to a Stream.
Definition: Address.cpp:408
lldb::addr_t GetFileAddress() const
Get the file address.
Definition: Address.cpp:293
lldb::addr_t GetOffset() const
Get the section relative offset value.
Definition: Address.h:329
uint32_t GetAddressByteSize() const
Returns the size in bytes of an address of the current architecture.
Definition: ArchSpec.cpp:691
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition: Stream.h:401
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:134
size_t PutChar(char ch)
Definition: Stream.cpp:131
const ArchSpec & GetArchitecture() const
Definition: Target.h:1012
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:82
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
void DumpAddressRange(llvm::raw_ostream &s, uint64_t lo_addr, uint64_t hi_addr, uint32_t addr_size, const char *prefix=nullptr, const char *suffix=nullptr)
Output an address range to this stream.
Definition: Stream.cpp:120
void DumpAddress(llvm::raw_ostream &s, uint64_t addr, uint32_t addr_size, const char *prefix=nullptr, const char *suffix=nullptr)
Output an address value to this stream.
Definition: Stream.cpp:108
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::Section > SectionSP
Definition: lldb-forward.h:406
uint64_t addr_t
Definition: lldb-types.h:79
std::shared_ptr< lldb_private::Module > ModuleSP
Definition: lldb-forward.h:365