Skip to content

Commit feb5a77

Browse files
authored
[lldb] Add SymbolContext::GetFunctionOrSymbolAddress (#123340)
Many uses of SC::GetAddressRange were not interested in the range, but in the address of the function/symbol contained inside the symbol context. They were getting that by calling the GetBaseAddress on the returned range, which worked well enough so far, but isn't compatible with discontinuous functions, whose address (entry point) may not be the lowest address in the range. To resolve this problem, this PR creates a new function whose purpose is return the address of the function or symbol inside the symbol context. It also changes all of the callers of GetAddressRange which do not actually care about the range to call this function instead.
1 parent 6e402f5 commit feb5a77

File tree

9 files changed

+55
-69
lines changed

9 files changed

+55
-69
lines changed

lldb/include/lldb/Symbol/SymbolContext.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ class SymbolContext {
165165
/// eSymbolContextSymbol is set in \a scope
166166
///
167167
/// \param[in] scope
168-
/// A mask of symbol context bits telling this function which
169-
/// address ranges it can use when trying to extract one from
168+
/// A mask bits from the \b SymbolContextItem enum telling this function
169+
/// which address ranges it can use when trying to extract one from
170170
/// the valid (non-nullptr) symbol context classes.
171171
///
172172
/// \param[in] range_idx
@@ -192,6 +192,13 @@ class SymbolContext {
192192
bool GetAddressRange(uint32_t scope, uint32_t range_idx,
193193
bool use_inline_block_range, AddressRange &range) const;
194194

195+
/// Get the address of the function or symbol represented by this symbol
196+
/// context.
197+
///
198+
/// If both fields are present, the address of the function is returned. If
199+
/// both are empty, the result is an invalid address.
200+
Address GetFunctionOrSymbolAddress() const;
201+
195202
llvm::Error GetAddressRangeFromHereToEndLine(uint32_t end_line,
196203
AddressRange &range);
197204

lldb/source/Commands/CommandObjectTarget.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,12 +1621,15 @@ static void DumpSymbolContextList(
16211621
if (!first_module)
16221622
strm.EOL();
16231623

1624-
AddressRange range;
1625-
1626-
sc.GetAddressRange(eSymbolContextEverything, 0, true, range);
1624+
Address addr;
1625+
if (sc.line_entry.IsValid())
1626+
addr = sc.line_entry.range.GetBaseAddress();
1627+
else if (sc.block && sc.block->GetContainingInlinedBlock())
1628+
sc.block->GetContainingInlinedBlock()->GetStartAddress(addr);
1629+
else
1630+
addr = sc.GetFunctionOrSymbolAddress();
16271631

1628-
DumpAddress(exe_scope, range.GetBaseAddress(), verbose, all_ranges, strm,
1629-
settings);
1632+
DumpAddress(exe_scope, addr, verbose, all_ranges, strm, settings);
16301633
first_module = false;
16311634
}
16321635
strm.IndentLess();
@@ -3570,16 +3573,13 @@ class CommandObjectTargetModulesShowUnwind : public CommandObjectParsed {
35703573
continue;
35713574
if (!sc.module_sp || sc.module_sp->GetObjectFile() == nullptr)
35723575
continue;
3573-
AddressRange range;
3574-
if (!sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0,
3575-
false, range))
3576-
continue;
3577-
if (!range.GetBaseAddress().IsValid())
3576+
Address addr = sc.GetFunctionOrSymbolAddress();
3577+
if (!addr.IsValid())
35783578
continue;
35793579
ConstString funcname(sc.GetFunctionName());
35803580
if (funcname.IsEmpty())
35813581
continue;
3582-
addr_t start_addr = range.GetBaseAddress().GetLoadAddress(target);
3582+
addr_t start_addr = addr.GetLoadAddress(target);
35833583
if (abi)
35843584
start_addr = abi->FixCodeAddress(start_addr);
35853585

lldb/source/Plugins/DynamicLoader/Hexagon-DYLD/DynamicLoaderHexagonDYLD.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -426,9 +426,7 @@ DynamicLoaderHexagonDYLD::GetStepThroughTrampolinePlan(Thread &thread,
426426
typedef std::vector<lldb::addr_t> AddressVector;
427427
AddressVector addrs;
428428
for (const SymbolContext &context : target_symbols) {
429-
AddressRange range;
430-
context.GetAddressRange(eSymbolContextEverything, 0, false, range);
431-
lldb::addr_t addr = range.GetBaseAddress().GetLoadAddress(&target);
429+
addr_t addr = context.GetFunctionOrSymbolAddress().GetLoadAddress(&target);
432430
if (addr != LLDB_INVALID_ADDRESS)
433431
addrs.push_back(addr);
434432
}

lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -939,13 +939,10 @@ DynamicLoaderDarwin::GetStepThroughTrampolinePlan(Thread &thread,
939939
images.FindSymbolsWithNameAndType(trampoline_name, eSymbolTypeCode,
940940
code_symbols);
941941
for (const SymbolContext &context : code_symbols) {
942-
AddressRange addr_range;
943-
context.GetAddressRange(eSymbolContextEverything, 0, false,
944-
addr_range);
945-
addresses.push_back(addr_range.GetBaseAddress());
942+
Address addr = context.GetFunctionOrSymbolAddress();
943+
addresses.push_back(addr);
946944
if (log) {
947-
addr_t load_addr =
948-
addr_range.GetBaseAddress().GetLoadAddress(target_sp.get());
945+
addr_t load_addr = addr.GetLoadAddress(target_sp.get());
949946

950947
LLDB_LOGF(log, "Found a trampoline target symbol at 0x%" PRIx64 ".",
951948
load_addr);
@@ -980,13 +977,10 @@ DynamicLoaderDarwin::GetStepThroughTrampolinePlan(Thread &thread,
980977
indirect_symbols);
981978

982979
for (const SymbolContext &context : indirect_symbols) {
983-
AddressRange addr_range;
984-
context.GetAddressRange(eSymbolContextEverything, 0, false,
985-
addr_range);
986-
addresses.push_back(addr_range.GetBaseAddress());
980+
Address addr = context.GetFunctionOrSymbolAddress();
981+
addresses.push_back(addr);
987982
if (log) {
988-
addr_t load_addr =
989-
addr_range.GetBaseAddress().GetLoadAddress(target_sp.get());
983+
addr_t load_addr = addr.GetLoadAddress(target_sp.get());
990984

991985
LLDB_LOGF(log, "Found an indirect target symbol at 0x%" PRIx64 ".",
992986
load_addr);

lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -512,9 +512,7 @@ DynamicLoaderPOSIXDYLD::GetStepThroughTrampolinePlan(Thread &thread,
512512
typedef std::vector<lldb::addr_t> AddressVector;
513513
AddressVector addrs;
514514
for (const SymbolContext &context : target_symbols) {
515-
AddressRange range;
516-
context.GetAddressRange(eSymbolContextEverything, 0, false, range);
517-
lldb::addr_t addr = range.GetBaseAddress().GetLoadAddress(&target);
515+
addr_t addr = context.GetFunctionOrSymbolAddress().GetLoadAddress(&target);
518516
if (addr != LLDB_INVALID_ADDRESS)
519517
addrs.push_back(addr);
520518
}

lldb/source/Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,7 @@ line_entry_helper(Target &target, const SymbolContext &sc, Symbol *symbol,
142142

143143
CPPLanguageRuntime::LibCppStdFunctionCallableInfo optional_info;
144144

145-
AddressRange range;
146-
sc.GetAddressRange(eSymbolContextEverything, 0, false, range);
147-
148-
Address address = range.GetBaseAddress();
145+
Address address = sc.GetFunctionOrSymbolAddress();
149146

150147
Address addr;
151148
if (target.ResolveLoadAddress(address.GetCallableLoadAddress(&target),

lldb/source/Plugins/Process/Utility/InferiorCallPOSIX.cpp

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ bool lldb_private::InferiorCallMmap(Process *process, addr_t &allocated_addr,
5252
if (count > 0) {
5353
SymbolContext sc;
5454
if (sc_list.GetContextAtIndex(0, sc)) {
55-
const uint32_t range_scope =
56-
eSymbolContextFunction | eSymbolContextSymbol;
57-
const bool use_inline_block_range = false;
5855
EvaluateExpressionOptions options;
5956
options.SetStopOthers(true);
6057
options.SetUnwindOnError(true);
@@ -77,9 +74,8 @@ bool lldb_private::InferiorCallMmap(Process *process, addr_t &allocated_addr,
7774
prot_arg |= PROT_WRITE;
7875
}
7976

80-
AddressRange mmap_range;
81-
if (sc.GetAddressRange(range_scope, 0, use_inline_block_range,
82-
mmap_range)) {
77+
Address mmap_addr = sc.GetFunctionOrSymbolAddress();
78+
if (mmap_addr.IsValid()) {
8379
auto type_system_or_err =
8480
process->GetTarget().GetScratchTypeSystemForLanguage(
8581
eLanguageTypeC);
@@ -96,9 +92,8 @@ bool lldb_private::InferiorCallMmap(Process *process, addr_t &allocated_addr,
9692
MmapArgList args =
9793
process->GetTarget().GetPlatform()->GetMmapArgumentList(
9894
arch, addr, length, prot_arg, flags, fd, offset);
99-
lldb::ThreadPlanSP call_plan_sp(
100-
new ThreadPlanCallFunction(*thread, mmap_range.GetBaseAddress(),
101-
void_ptr_type, args, options));
95+
lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallFunction(
96+
*thread, mmap_addr, void_ptr_type, args, options));
10297
if (call_plan_sp) {
10398
DiagnosticManager diagnostics;
10499

@@ -149,9 +144,6 @@ bool lldb_private::InferiorCallMunmap(Process *process, addr_t addr,
149144
if (count > 0) {
150145
SymbolContext sc;
151146
if (sc_list.GetContextAtIndex(0, sc)) {
152-
const uint32_t range_scope =
153-
eSymbolContextFunction | eSymbolContextSymbol;
154-
const bool use_inline_block_range = false;
155147
EvaluateExpressionOptions options;
156148
options.SetStopOthers(true);
157149
options.SetUnwindOnError(true);
@@ -161,13 +153,11 @@ bool lldb_private::InferiorCallMunmap(Process *process, addr_t addr,
161153
options.SetTimeout(process->GetUtilityExpressionTimeout());
162154
options.SetTrapExceptions(false);
163155

164-
AddressRange munmap_range;
165-
if (sc.GetAddressRange(range_scope, 0, use_inline_block_range,
166-
munmap_range)) {
156+
Address munmap_addr = sc.GetFunctionOrSymbolAddress();
157+
if (munmap_addr.IsValid()) {
167158
lldb::addr_t args[] = {addr, length};
168-
lldb::ThreadPlanSP call_plan_sp(
169-
new ThreadPlanCallFunction(*thread, munmap_range.GetBaseAddress(),
170-
CompilerType(), args, options));
159+
lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallFunction(
160+
*thread, munmap_addr, CompilerType(), args, options));
171161
if (call_plan_sp) {
172162
DiagnosticManager diagnostics;
173163

lldb/source/Plugins/SystemRuntime/MacOSX/SystemRuntimeMacOSX.cpp

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -632,10 +632,8 @@ bool SystemRuntimeMacOSX::BacktraceRecordingHeadersInitialized() {
632632
if (!sc_list.IsEmpty()) {
633633
SymbolContext sc;
634634
sc_list.GetContextAtIndex(0, sc);
635-
AddressRange addr_range;
636-
sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range);
637-
queue_info_version_address =
638-
addr_range.GetBaseAddress().GetLoadAddress(&target);
635+
Address addr = sc.GetFunctionOrSymbolAddress();
636+
queue_info_version_address = addr.GetLoadAddress(&target);
639637
}
640638
sc_list.Clear();
641639

@@ -646,10 +644,8 @@ bool SystemRuntimeMacOSX::BacktraceRecordingHeadersInitialized() {
646644
if (!sc_list.IsEmpty()) {
647645
SymbolContext sc;
648646
sc_list.GetContextAtIndex(0, sc);
649-
AddressRange addr_range;
650-
sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range);
651-
queue_info_data_offset_address =
652-
addr_range.GetBaseAddress().GetLoadAddress(&target);
647+
Address addr = sc.GetFunctionOrSymbolAddress();
648+
queue_info_data_offset_address = addr.GetLoadAddress(&target);
653649
}
654650
sc_list.Clear();
655651

@@ -660,10 +656,8 @@ bool SystemRuntimeMacOSX::BacktraceRecordingHeadersInitialized() {
660656
if (!sc_list.IsEmpty()) {
661657
SymbolContext sc;
662658
sc_list.GetContextAtIndex(0, sc);
663-
AddressRange addr_range;
664-
sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range);
665-
item_info_version_address =
666-
addr_range.GetBaseAddress().GetLoadAddress(&target);
659+
Address addr = sc.GetFunctionOrSymbolAddress();
660+
item_info_version_address = addr.GetLoadAddress(&target);
667661
}
668662
sc_list.Clear();
669663

@@ -674,10 +668,8 @@ bool SystemRuntimeMacOSX::BacktraceRecordingHeadersInitialized() {
674668
if (!sc_list.IsEmpty()) {
675669
SymbolContext sc;
676670
sc_list.GetContextAtIndex(0, sc);
677-
AddressRange addr_range;
678-
sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range);
679-
item_info_data_offset_address =
680-
addr_range.GetBaseAddress().GetLoadAddress(&target);
671+
Address addr = sc.GetFunctionOrSymbolAddress();
672+
item_info_data_offset_address = addr.GetLoadAddress(&target);
681673
}
682674

683675
if (queue_info_version_address != LLDB_INVALID_ADDRESS &&

lldb/source/Symbol/SymbolContext.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,16 @@ bool SymbolContext::GetAddressRange(uint32_t scope, uint32_t range_idx,
370370
return false;
371371
}
372372

373+
Address SymbolContext::GetFunctionOrSymbolAddress() const {
374+
if (function)
375+
return function->GetAddress();
376+
377+
if (symbol)
378+
return symbol->GetAddress();
379+
380+
return Address();
381+
}
382+
373383
LanguageType SymbolContext::GetLanguage() const {
374384
LanguageType lang;
375385
if (function && (lang = function->GetLanguage()) != eLanguageTypeUnknown) {

0 commit comments

Comments
 (0)