Skip to content

[lldb] Add SymbolContext::GetFunctionOrSymbolAddress #123340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lldb/include/lldb/Symbol/SymbolContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ class SymbolContext {
/// eSymbolContextSymbol is set in \a scope
///
/// \param[in] scope
/// A mask of symbol context bits telling this function which
/// address ranges it can use when trying to extract one from
/// A mask bits from the \b SymbolContextItem enum telling this function
/// which address ranges it can use when trying to extract one from
/// the valid (non-nullptr) symbol context classes.
///
/// \param[in] range_idx
Expand All @@ -192,6 +192,13 @@ class SymbolContext {
bool GetAddressRange(uint32_t scope, uint32_t range_idx,
bool use_inline_block_range, AddressRange &range) const;

/// Get the address of the function or symbol represented by this symbol
/// context.
///
/// If both fields are present, the address of the function is returned. If
/// both are empty, the result is an invalid address.
Address GetFunctionOrSymbolAddress() const;

llvm::Error GetAddressRangeFromHereToEndLine(uint32_t end_line,
AddressRange &range);

Expand Down
22 changes: 11 additions & 11 deletions lldb/source/Commands/CommandObjectTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1621,12 +1621,15 @@ static void DumpSymbolContextList(
if (!first_module)
strm.EOL();

AddressRange range;

sc.GetAddressRange(eSymbolContextEverything, 0, true, range);
Address addr;
if (sc.line_entry.IsValid())
addr = sc.line_entry.range.GetBaseAddress();
else if (sc.block && sc.block->GetContainingInlinedBlock())
sc.block->GetContainingInlinedBlock()->GetStartAddress(addr);
else
addr = sc.GetFunctionOrSymbolAddress();

DumpAddress(exe_scope, range.GetBaseAddress(), verbose, all_ranges, strm,
settings);
DumpAddress(exe_scope, addr, verbose, all_ranges, strm, settings);
first_module = false;
}
strm.IndentLess();
Expand Down Expand Up @@ -3570,16 +3573,13 @@ class CommandObjectTargetModulesShowUnwind : public CommandObjectParsed {
continue;
if (!sc.module_sp || sc.module_sp->GetObjectFile() == nullptr)
continue;
AddressRange range;
if (!sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0,
false, range))
continue;
if (!range.GetBaseAddress().IsValid())
Address addr = sc.GetFunctionOrSymbolAddress();
if (!addr.IsValid())
continue;
ConstString funcname(sc.GetFunctionName());
if (funcname.IsEmpty())
continue;
addr_t start_addr = range.GetBaseAddress().GetLoadAddress(target);
addr_t start_addr = addr.GetLoadAddress(target);
if (abi)
start_addr = abi->FixCodeAddress(start_addr);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,9 +426,7 @@ DynamicLoaderHexagonDYLD::GetStepThroughTrampolinePlan(Thread &thread,
typedef std::vector<lldb::addr_t> AddressVector;
AddressVector addrs;
for (const SymbolContext &context : target_symbols) {
AddressRange range;
context.GetAddressRange(eSymbolContextEverything, 0, false, range);
lldb::addr_t addr = range.GetBaseAddress().GetLoadAddress(&target);
addr_t addr = context.GetFunctionOrSymbolAddress().GetLoadAddress(&target);
if (addr != LLDB_INVALID_ADDRESS)
addrs.push_back(addr);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -939,13 +939,10 @@ DynamicLoaderDarwin::GetStepThroughTrampolinePlan(Thread &thread,
images.FindSymbolsWithNameAndType(trampoline_name, eSymbolTypeCode,
code_symbols);
for (const SymbolContext &context : code_symbols) {
AddressRange addr_range;
context.GetAddressRange(eSymbolContextEverything, 0, false,
addr_range);
addresses.push_back(addr_range.GetBaseAddress());
Address addr = context.GetFunctionOrSymbolAddress();
addresses.push_back(addr);
if (log) {
addr_t load_addr =
addr_range.GetBaseAddress().GetLoadAddress(target_sp.get());
addr_t load_addr = addr.GetLoadAddress(target_sp.get());

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

for (const SymbolContext &context : indirect_symbols) {
AddressRange addr_range;
context.GetAddressRange(eSymbolContextEverything, 0, false,
addr_range);
addresses.push_back(addr_range.GetBaseAddress());
Address addr = context.GetFunctionOrSymbolAddress();
addresses.push_back(addr);
if (log) {
addr_t load_addr =
addr_range.GetBaseAddress().GetLoadAddress(target_sp.get());
addr_t load_addr = addr.GetLoadAddress(target_sp.get());

LLDB_LOGF(log, "Found an indirect target symbol at 0x%" PRIx64 ".",
load_addr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,9 +512,7 @@ DynamicLoaderPOSIXDYLD::GetStepThroughTrampolinePlan(Thread &thread,
typedef std::vector<lldb::addr_t> AddressVector;
AddressVector addrs;
for (const SymbolContext &context : target_symbols) {
AddressRange range;
context.GetAddressRange(eSymbolContextEverything, 0, false, range);
lldb::addr_t addr = range.GetBaseAddress().GetLoadAddress(&target);
addr_t addr = context.GetFunctionOrSymbolAddress().GetLoadAddress(&target);
if (addr != LLDB_INVALID_ADDRESS)
addrs.push_back(addr);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,7 @@ line_entry_helper(Target &target, const SymbolContext &sc, Symbol *symbol,

CPPLanguageRuntime::LibCppStdFunctionCallableInfo optional_info;

AddressRange range;
sc.GetAddressRange(eSymbolContextEverything, 0, false, range);

Address address = range.GetBaseAddress();
Address address = sc.GetFunctionOrSymbolAddress();

Address addr;
if (target.ResolveLoadAddress(address.GetCallableLoadAddress(&target),
Expand Down
26 changes: 8 additions & 18 deletions lldb/source/Plugins/Process/Utility/InferiorCallPOSIX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ bool lldb_private::InferiorCallMmap(Process *process, addr_t &allocated_addr,
if (count > 0) {
SymbolContext sc;
if (sc_list.GetContextAtIndex(0, sc)) {
const uint32_t range_scope =
eSymbolContextFunction | eSymbolContextSymbol;
const bool use_inline_block_range = false;
EvaluateExpressionOptions options;
options.SetStopOthers(true);
options.SetUnwindOnError(true);
Expand All @@ -77,9 +74,8 @@ bool lldb_private::InferiorCallMmap(Process *process, addr_t &allocated_addr,
prot_arg |= PROT_WRITE;
}

AddressRange mmap_range;
if (sc.GetAddressRange(range_scope, 0, use_inline_block_range,
mmap_range)) {
Address mmap_addr = sc.GetFunctionOrSymbolAddress();
if (mmap_addr.IsValid()) {
auto type_system_or_err =
process->GetTarget().GetScratchTypeSystemForLanguage(
eLanguageTypeC);
Expand All @@ -96,9 +92,8 @@ bool lldb_private::InferiorCallMmap(Process *process, addr_t &allocated_addr,
MmapArgList args =
process->GetTarget().GetPlatform()->GetMmapArgumentList(
arch, addr, length, prot_arg, flags, fd, offset);
lldb::ThreadPlanSP call_plan_sp(
new ThreadPlanCallFunction(*thread, mmap_range.GetBaseAddress(),
void_ptr_type, args, options));
lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallFunction(
*thread, mmap_addr, void_ptr_type, args, options));
if (call_plan_sp) {
DiagnosticManager diagnostics;

Expand Down Expand Up @@ -149,9 +144,6 @@ bool lldb_private::InferiorCallMunmap(Process *process, addr_t addr,
if (count > 0) {
SymbolContext sc;
if (sc_list.GetContextAtIndex(0, sc)) {
const uint32_t range_scope =
eSymbolContextFunction | eSymbolContextSymbol;
const bool use_inline_block_range = false;
EvaluateExpressionOptions options;
options.SetStopOthers(true);
options.SetUnwindOnError(true);
Expand All @@ -161,13 +153,11 @@ bool lldb_private::InferiorCallMunmap(Process *process, addr_t addr,
options.SetTimeout(process->GetUtilityExpressionTimeout());
options.SetTrapExceptions(false);

AddressRange munmap_range;
if (sc.GetAddressRange(range_scope, 0, use_inline_block_range,
munmap_range)) {
Address munmap_addr = sc.GetFunctionOrSymbolAddress();
if (munmap_addr.IsValid()) {
lldb::addr_t args[] = {addr, length};
lldb::ThreadPlanSP call_plan_sp(
new ThreadPlanCallFunction(*thread, munmap_range.GetBaseAddress(),
CompilerType(), args, options));
lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallFunction(
*thread, munmap_addr, CompilerType(), args, options));
if (call_plan_sp) {
DiagnosticManager diagnostics;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -632,10 +632,8 @@ bool SystemRuntimeMacOSX::BacktraceRecordingHeadersInitialized() {
if (!sc_list.IsEmpty()) {
SymbolContext sc;
sc_list.GetContextAtIndex(0, sc);
AddressRange addr_range;
sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range);
queue_info_version_address =
addr_range.GetBaseAddress().GetLoadAddress(&target);
Address addr = sc.GetFunctionOrSymbolAddress();
queue_info_version_address = addr.GetLoadAddress(&target);
}
sc_list.Clear();

Expand All @@ -646,10 +644,8 @@ bool SystemRuntimeMacOSX::BacktraceRecordingHeadersInitialized() {
if (!sc_list.IsEmpty()) {
SymbolContext sc;
sc_list.GetContextAtIndex(0, sc);
AddressRange addr_range;
sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range);
queue_info_data_offset_address =
addr_range.GetBaseAddress().GetLoadAddress(&target);
Address addr = sc.GetFunctionOrSymbolAddress();
queue_info_data_offset_address = addr.GetLoadAddress(&target);
}
sc_list.Clear();

Expand All @@ -660,10 +656,8 @@ bool SystemRuntimeMacOSX::BacktraceRecordingHeadersInitialized() {
if (!sc_list.IsEmpty()) {
SymbolContext sc;
sc_list.GetContextAtIndex(0, sc);
AddressRange addr_range;
sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range);
item_info_version_address =
addr_range.GetBaseAddress().GetLoadAddress(&target);
Address addr = sc.GetFunctionOrSymbolAddress();
item_info_version_address = addr.GetLoadAddress(&target);
}
sc_list.Clear();

Expand All @@ -674,10 +668,8 @@ bool SystemRuntimeMacOSX::BacktraceRecordingHeadersInitialized() {
if (!sc_list.IsEmpty()) {
SymbolContext sc;
sc_list.GetContextAtIndex(0, sc);
AddressRange addr_range;
sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range);
item_info_data_offset_address =
addr_range.GetBaseAddress().GetLoadAddress(&target);
Address addr = sc.GetFunctionOrSymbolAddress();
item_info_data_offset_address = addr.GetLoadAddress(&target);
}

if (queue_info_version_address != LLDB_INVALID_ADDRESS &&
Expand Down
10 changes: 10 additions & 0 deletions lldb/source/Symbol/SymbolContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,16 @@ bool SymbolContext::GetAddressRange(uint32_t scope, uint32_t range_idx,
return false;
}

Address SymbolContext::GetFunctionOrSymbolAddress() const {
if (function)
return function->GetAddress();

if (symbol)
return symbol->GetAddress();

return Address();
}

LanguageType SymbolContext::GetLanguage() const {
LanguageType lang;
if (function && (lang = function->GetLanguage()) != eLanguageTypeUnknown) {
Expand Down
Loading