Skip to content

Commit 86e61fb

Browse files
committed
[lldb] Turn LineEntry into a class and make AddressRange member optional
This patch relaxes the LineEntry validity from requiring a valid AddressRange to turning the LineEntry `struct` into a `class` and make the AddressRange member a private optional. This addresses a problem for when LineEntry are created from the SB API, because there is no way of adding an address range to an SBLineEntry, it's always invalid. This becomes a bigger problem when trying to set the line entry into a SymbolContext object and it discards it after checking its validity. This patch is necessary to extend ScriptedFrames ability to create synthetic frames with invalid addresses and invalid line entries. Signed-off-by: Med Ismail Bennani <[email protected]>
1 parent 49a9787 commit 86e61fb

25 files changed

+200
-102
lines changed

lldb/include/lldb/Breakpoint/BreakpointLocation.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,8 @@ class BreakpointLocation
308308
/// The line entry must have the same start address as the address for this
309309
/// location.
310310
bool SetPreferredLineEntry(const LineEntry &line_entry) {
311-
if (m_address == line_entry.range.GetBaseAddress()) {
311+
if (line_entry.HasValidRange() &&
312+
m_address == line_entry.GetRange().GetBaseAddress()) {
312313
m_preferred_line_entry = line_entry;
313314
return true;
314315
}

lldb/include/lldb/Symbol/LineEntry.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "lldb/Utility/FileSpec.h"
1414
#include "lldb/Utility/SupportFile.h"
1515
#include "lldb/lldb-private.h"
16+
#include <optional>
1617

1718
namespace lldb_private {
1819

@@ -133,8 +134,15 @@ struct LineEntry {
133134
/// Helper to access the file.
134135
const FileSpec &GetFile() const { return file_sp->GetSpecOnly(); }
135136

136-
/// The section offset address range for this line entry.
137-
AddressRange range;
137+
/// Get the address range for this line entry.
138+
/// \return The address range if valid, otherwise an invalid AddressRange.
139+
const AddressRange &GetRange() const;
140+
141+
/// Check if this line entry has a valid address range.
142+
bool HasValidRange() const;
143+
144+
/// Set the address range for this line entry.
145+
void SetRange(const AddressRange &range);
138146

139147
/// The source file, possibly mapped by the target.source-map setting.
140148
SupportFileNSP file_sp;
@@ -167,6 +175,10 @@ struct LineEntry {
167175
/// Indicates this entry is that of the first byte after the end of a sequence
168176
/// of target machine instructions.
169177
uint16_t is_terminal_entry : 1;
178+
179+
private:
180+
/// The section offset address range for this line entry (optional).
181+
std::optional<AddressRange> m_range;
170182
};
171183

172184
/// Less than operator.

lldb/source/API/SBLineEntry.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ SBAddress SBLineEntry::GetStartAddress() const {
5050
LLDB_INSTRUMENT_VA(this);
5151

5252
SBAddress sb_address;
53-
if (m_opaque_up)
54-
sb_address.SetAddress(m_opaque_up->range.GetBaseAddress());
53+
if (m_opaque_up && m_opaque_up->HasValidRange())
54+
sb_address.SetAddress(m_opaque_up->GetRange().GetBaseAddress());
5555

5656
return sb_address;
5757
}
@@ -60,9 +60,9 @@ SBAddress SBLineEntry::GetEndAddress() const {
6060
LLDB_INSTRUMENT_VA(this);
6161

6262
SBAddress sb_address;
63-
if (m_opaque_up) {
64-
sb_address.SetAddress(m_opaque_up->range.GetBaseAddress());
65-
sb_address.OffsetAddress(m_opaque_up->range.GetByteSize());
63+
if (m_opaque_up && m_opaque_up->HasValidRange()) {
64+
sb_address.SetAddress(m_opaque_up->GetRange().GetBaseAddress());
65+
sb_address.OffsetAddress(m_opaque_up->GetRange().GetByteSize());
6666
}
6767
return sb_address;
6868
}

lldb/source/API/SBThread.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -539,9 +539,14 @@ void SBThread::StepInto(const char *target_name, uint32_t end_line,
539539
if (frame_sp && frame_sp->HasDebugInformation()) {
540540
SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
541541
AddressRange range;
542-
if (end_line == LLDB_INVALID_LINE_NUMBER)
543-
range = sc.line_entry.range;
544-
else {
542+
if (end_line == LLDB_INVALID_LINE_NUMBER) {
543+
if (sc.line_entry.HasValidRange())
544+
range = sc.line_entry.GetRange();
545+
else {
546+
error = Status::FromErrorString("No valid range for line entry");
547+
return;
548+
}
549+
} else {
545550
llvm::Error err = sc.GetAddressRangeFromHereToEndLine(end_line, range);
546551
if (err) {
547552
error = Status::FromErrorString(llvm::toString(std::move(err)).c_str());
@@ -811,7 +816,7 @@ SBError SBThread::StepOverUntil(lldb::SBFrame &sb_frame,
811816
eSymbolContextLineEntry, sc_list);
812817
for (const SymbolContext &sc : sc_list) {
813818
addr_t step_addr =
814-
sc.line_entry.range.GetBaseAddress().GetLoadAddress(target);
819+
sc.line_entry.GetRange().GetBaseAddress().GetLoadAddress(target);
815820
if (step_addr != LLDB_INVALID_ADDRESS) {
816821
AddressRange unused_range;
817822
if (frame_sc.function->GetRangeContainingLoadAddress(step_addr, *target,

lldb/source/Breakpoint/BreakpointResolver.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,19 @@ void BreakpointResolver::SetSCMatchesByLine(
273273
}
274274

275275
// Sort by file address.
276-
llvm::sort(worklist_begin, worklist_end,
277-
[](const SymbolContext &a, const SymbolContext &b) {
278-
return a.line_entry.range.GetBaseAddress().GetFileAddress() <
279-
b.line_entry.range.GetBaseAddress().GetFileAddress();
280-
});
276+
llvm::sort(
277+
worklist_begin, worklist_end,
278+
[](const SymbolContext &a, const SymbolContext &b) {
279+
auto a_addr =
280+
a.line_entry.HasValidRange()
281+
? a.line_entry.GetRange().GetBaseAddress().GetFileAddress()
282+
: LLDB_INVALID_ADDRESS;
283+
auto b_addr =
284+
b.line_entry.HasValidRange()
285+
? b.line_entry.GetRange().GetBaseAddress().GetFileAddress()
286+
: LLDB_INVALID_ADDRESS;
287+
return a_addr < b_addr;
288+
});
281289

282290
// Go through and see if there are line table entries that are
283291
// contiguous, and if so keep only the first of the contiguous range.
@@ -307,7 +315,9 @@ void BreakpointResolver::AddLocation(SearchFilter &filter,
307315
bool skip_prologue,
308316
llvm::StringRef log_ident) {
309317
Log *log = GetLog(LLDBLog::Breakpoints);
310-
Address line_start = sc.line_entry.range.GetBaseAddress();
318+
Address line_start = sc.line_entry.HasValidRange()
319+
? sc.line_entry.GetRange().GetBaseAddress()
320+
: Address();
311321
if (!line_start.IsValid()) {
312322
LLDB_LOGF(log,
313323
"error: Unable to set breakpoint %s at file address "

lldb/source/Commands/CommandObjectDisassemble.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,8 @@ CommandObjectDisassemble::GetCurrentLineRanges() {
356356

357357
LineEntry pc_line_entry(
358358
frame->GetSymbolContext(eSymbolContextLineEntry).line_entry);
359-
if (pc_line_entry.IsValid())
360-
return std::vector<AddressRange>{pc_line_entry.range};
359+
if (pc_line_entry.IsValid() && pc_line_entry.HasValidRange())
360+
return std::vector<AddressRange>{pc_line_entry.GetRange()};
361361

362362
// No line entry, so just disassemble around the current pc
363363
m_options.show_mixed = false;

lldb/source/Commands/CommandObjectSource.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,9 +1032,11 @@ class CommandObjectSourceList : public CommandObjectParsed {
10321032
bool show_inlined_frames = true;
10331033
const bool show_function_arguments = true;
10341034
const bool show_function_name = true;
1035+
Address addr = sc.line_entry.HasValidRange()
1036+
? sc.line_entry.GetRange().GetBaseAddress()
1037+
: Address();
10351038
sc.DumpStopContext(&result.GetOutputStream(),
1036-
m_exe_ctx.GetBestExecutionContextScope(),
1037-
sc.line_entry.range.GetBaseAddress(),
1039+
m_exe_ctx.GetBestExecutionContextScope(), addr,
10381040
show_fullpaths, show_module, show_inlined_frames,
10391041
show_function_arguments, show_function_name);
10401042
result.GetOutputStream().EOL();

lldb/source/Commands/CommandObjectTarget.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,8 +1634,8 @@ static void DumpSymbolContextList(
16341634
strm.EOL();
16351635

16361636
Address addr;
1637-
if (sc.line_entry.IsValid())
1638-
addr = sc.line_entry.range.GetBaseAddress();
1637+
if (sc.line_entry.IsValid() && sc.line_entry.HasValidRange())
1638+
addr = sc.line_entry.GetRange().GetBaseAddress();
16391639
else if (sc.block && sc.block->GetContainingInlinedBlock())
16401640
sc.block->GetContainingInlinedBlock()->GetStartAddress(addr);
16411641
else

lldb/source/Commands/CommandObjectThread.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ class CommandObjectThreadStepWithTypeAndScope : public CommandObjectParsed {
519519
block_range.GetByteSize() - pc_offset_in_block;
520520
range = AddressRange(pc_address, range_length);
521521
} else {
522-
range = sc.line_entry.range;
522+
range = sc.line_entry.GetRange();
523523
}
524524

525525
new_plan_sp = thread->QueueThreadPlanForStepInRange(
@@ -999,7 +999,7 @@ class CommandObjectThreadUntil : public CommandObjectParsed {
999999
while (idx < end_func_idx) {
10001000
if (line_idx_ranges.FindEntryIndexThatContains(idx) != UINT32_MAX) {
10011001
addr_t address =
1002-
line_entry.range.GetBaseAddress().GetLoadAddress(target);
1002+
line_entry.GetRange().GetBaseAddress().GetLoadAddress(target);
10031003
if (address != LLDB_INVALID_ADDRESS)
10041004
address_list.push_back(address);
10051005
}

lldb/source/Core/AddressResolverFileLine.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ AddressResolverFileLine::SearchCallback(SearchFilter &filter,
4646
cu->ResolveSymbolContext(m_src_location_spec, eSymbolContextEverything,
4747
sc_list);
4848
for (const SymbolContext &sc : sc_list) {
49-
Address line_start = sc.line_entry.range.GetBaseAddress();
50-
addr_t byte_size = sc.line_entry.range.GetByteSize();
49+
Address line_start = sc.line_entry.GetRange().GetBaseAddress();
50+
addr_t byte_size = sc.line_entry.GetRange().GetByteSize();
5151
if (line_start.IsValid()) {
5252
AddressRange new_range(line_start, byte_size);
5353
m_address_ranges.push_back(new_range);

0 commit comments

Comments
 (0)