[Lldb-commits] [lldb] [lldb] Add SymbolContext::GetAddress (PR #123340)

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Mon Jan 20 04:11:21 PST 2025


https://github.com/labath updated https://github.com/llvm/llvm-project/pull/123340

>From 2d6210ad9527df5147987f856e941e61d9851a97 Mon Sep 17 00:00:00 2001
From: Pavel Labath <pavel at labath.sk>
Date: Fri, 17 Jan 2025 12:36:36 +0100
Subject: [PATCH 1/3] [lldb] Add SymbolContext::GetAddress

Many (most?) 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 object inside the symbol context ("address of
a block" is a somewhat fuzzy concept, but I've made it so that mirrors
what the GetAddressRange function does). It also changes all of the
callers of GetAddressRange which do not actually care about the range to
call this function instead.

I've also changed all of the callers which are interested in the entry
point of a function/symbol to pass eSymbolContextFunction |
eSymbolContextSymbol instead of eSymbolContextEverything, as a block or
a line will not have a callable address.
---
 lldb/include/lldb/Symbol/SymbolContext.h      | 27 ++++++++++++++++
 lldb/source/Commands/CommandObjectTarget.cpp  | 17 +++-------
 .../Hexagon-DYLD/DynamicLoaderHexagonDYLD.cpp |  6 ++--
 .../MacOSX-DYLD/DynamicLoaderDarwin.cpp       | 20 +++++-------
 .../POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp     |  6 ++--
 .../CPlusPlus/CPPLanguageRuntime.cpp          |  6 ++--
 .../Process/Utility/InferiorCallPOSIX.cpp     | 32 ++++++++-----------
 .../MacOSX/SystemRuntimeMacOSX.cpp            | 24 +++++---------
 lldb/source/Symbol/SymbolContext.cpp          | 25 +++++++++++++++
 9 files changed, 95 insertions(+), 68 deletions(-)

diff --git a/lldb/include/lldb/Symbol/SymbolContext.h b/lldb/include/lldb/Symbol/SymbolContext.h
index 07769cd8dffae7..fde11f275dc60a 100644
--- a/lldb/include/lldb/Symbol/SymbolContext.h
+++ b/lldb/include/lldb/Symbol/SymbolContext.h
@@ -192,6 +192,33 @@ class SymbolContext {
   bool GetAddressRange(uint32_t scope, uint32_t range_idx,
                        bool use_inline_block_range, AddressRange &range) const;
 
+  /// Get the address represented by this symbol context.
+  ///
+  /// The exact meaning of the address depends on object being queried and the
+  /// flags. Priority is as follows:
+  ///     - The beginning (lowest address) of the line_entry if line_entry is
+  ///     valid and eSymbolContextLineEntry is set in \a scope
+  ///     - The beginning of the block if block is not nullptr and
+  ///     eSymbolContextBlock is set in \a scope
+  ///     - function address (entry point) if function is not nullptr and
+  ///     eSymbolContextFunction is set in \a scope
+  ///     - symbol address if symbol is not nullptr and 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
+  ///     the valid (non-nullptr) symbol context classes.
+  ///
+  /// \param[in] use_inline_block_range
+  ///     If \a scope has the eSymbolContextBlock bit set, and there
+  ///     is a valid block in the symbol context, return the block
+  ///     address range for the containing inline function block, not
+  ///     the deepest most block. This allows us to extract information
+  ///     for the address range of the inlined function block, not
+  ///     the deepest lexical block.
+  Address GetAddress(uint32_t scope, bool use_inline_block_range) const;
+
   llvm::Error GetAddressRangeFromHereToEndLine(uint32_t end_line,
                                                AddressRange &range);
 
diff --git a/lldb/source/Commands/CommandObjectTarget.cpp b/lldb/source/Commands/CommandObjectTarget.cpp
index d8265e41a7384e..705c69167badd3 100644
--- a/lldb/source/Commands/CommandObjectTarget.cpp
+++ b/lldb/source/Commands/CommandObjectTarget.cpp
@@ -1621,12 +1621,8 @@ static void DumpSymbolContextList(
     if (!first_module)
       strm.EOL();
 
-    AddressRange range;
-
-    sc.GetAddressRange(eSymbolContextEverything, 0, true, range);
-
-    DumpAddress(exe_scope, range.GetBaseAddress(), verbose, all_ranges, strm,
-                settings);
+    Address addr = sc.GetAddress(eSymbolContextEverything, true);
+    DumpAddress(exe_scope, addr, verbose, all_ranges, strm, settings);
     first_module = false;
   }
   strm.IndentLess();
@@ -3570,16 +3566,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.GetAddress(eSymbolContextFunction | eSymbolContextSymbol, false);
+      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);
 
diff --git a/lldb/source/Plugins/DynamicLoader/Hexagon-DYLD/DynamicLoaderHexagonDYLD.cpp b/lldb/source/Plugins/DynamicLoader/Hexagon-DYLD/DynamicLoaderHexagonDYLD.cpp
index 96c94535c623c7..a724af973bf80a 100644
--- a/lldb/source/Plugins/DynamicLoader/Hexagon-DYLD/DynamicLoaderHexagonDYLD.cpp
+++ b/lldb/source/Plugins/DynamicLoader/Hexagon-DYLD/DynamicLoaderHexagonDYLD.cpp
@@ -426,9 +426,9 @@ 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.GetAddress(eSymbolContextFunction | eSymbolContextSymbol, false)
+            .GetLoadAddress(&target);
     if (addr != LLDB_INVALID_ADDRESS)
       addrs.push_back(addr);
   }
diff --git a/lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.cpp b/lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.cpp
index 8949b14c571893..7aa449ba359d44 100644
--- a/lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.cpp
+++ b/lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.cpp
@@ -939,13 +939,11 @@ 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.GetAddress(
+              eSymbolContextFunction | eSymbolContextSymbol, false);
+          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);
@@ -980,13 +978,11 @@ 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.GetAddress(
+              eSymbolContextSymbol | eSymbolContextFunction, false);
+          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);
diff --git a/lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp b/lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp
index 5614bc32468d5c..364e50e3bc5243 100644
--- a/lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp
+++ b/lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp
@@ -512,9 +512,9 @@ 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.GetAddress(eSymbolContextSymbol | eSymbolContextFunction, false)
+            .GetLoadAddress(&target);
     if (addr != LLDB_INVALID_ADDRESS)
       addrs.push_back(addr);
   }
diff --git a/lldb/source/Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.cpp b/lldb/source/Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.cpp
index fb706544ea560d..613733de0b2e6d 100644
--- a/lldb/source/Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.cpp
+++ b/lldb/source/Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.cpp
@@ -142,10 +142,8 @@ 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.GetAddress(eSymbolContextFunction | eSymbolContextSymbol, false);
 
   Address addr;
   if (target.ResolveLoadAddress(address.GetCallableLoadAddress(&target),
diff --git a/lldb/source/Plugins/Process/Utility/InferiorCallPOSIX.cpp b/lldb/source/Plugins/Process/Utility/InferiorCallPOSIX.cpp
index 99426c7d1f261d..ae1d371c080376 100644
--- a/lldb/source/Plugins/Process/Utility/InferiorCallPOSIX.cpp
+++ b/lldb/source/Plugins/Process/Utility/InferiorCallPOSIX.cpp
@@ -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);
@@ -77,9 +74,11 @@ 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)) {
+      const uint32_t range_scope =
+          eSymbolContextFunction | eSymbolContextSymbol;
+      const bool use_inline_block_range = false;
+      Address mmap_addr = sc.GetAddress(range_scope, use_inline_block_range);
+      if (mmap_addr.IsValid()) {
         auto type_system_or_err =
             process->GetTarget().GetScratchTypeSystemForLanguage(
                 eLanguageTypeC);
@@ -96,9 +95,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;
 
@@ -149,9 +147,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);
@@ -161,13 +156,14 @@ 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)) {
+      const uint32_t range_scope =
+          eSymbolContextFunction | eSymbolContextSymbol;
+      const bool use_inline_block_range = false;
+      Address munmap_addr = sc.GetAddress(range_scope, use_inline_block_range);
+      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;
 
diff --git a/lldb/source/Plugins/SystemRuntime/MacOSX/SystemRuntimeMacOSX.cpp b/lldb/source/Plugins/SystemRuntime/MacOSX/SystemRuntimeMacOSX.cpp
index 0556b877778847..0893e8beaf4fe0 100644
--- a/lldb/source/Plugins/SystemRuntime/MacOSX/SystemRuntimeMacOSX.cpp
+++ b/lldb/source/Plugins/SystemRuntime/MacOSX/SystemRuntimeMacOSX.cpp
@@ -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.GetAddress(eSymbolContextSymbol, false);
+    queue_info_version_address = addr.GetLoadAddress(&target);
   }
   sc_list.Clear();
 
@@ -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.GetAddress(eSymbolContextSymbol, false);
+    queue_info_data_offset_address = addr.GetLoadAddress(&target);
   }
   sc_list.Clear();
 
@@ -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.GetAddress(eSymbolContextSymbol, false);
+    item_info_version_address = addr.GetLoadAddress(&target);
   }
   sc_list.Clear();
 
@@ -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.GetAddress(eSymbolContextSymbol, false);
+    item_info_data_offset_address = addr.GetLoadAddress(&target);
   }
 
   if (queue_info_version_address != LLDB_INVALID_ADDRESS &&
diff --git a/lldb/source/Symbol/SymbolContext.cpp b/lldb/source/Symbol/SymbolContext.cpp
index f4270ee8396760..0732dc8da8deed 100644
--- a/lldb/source/Symbol/SymbolContext.cpp
+++ b/lldb/source/Symbol/SymbolContext.cpp
@@ -370,6 +370,31 @@ bool SymbolContext::GetAddressRange(uint32_t scope, uint32_t range_idx,
   return false;
 }
 
+Address SymbolContext::GetAddress(uint32_t scope,
+                                  bool use_inline_block_range) const {
+  if ((scope & eSymbolContextLineEntry) && line_entry.IsValid())
+    return line_entry.range.GetBaseAddress();
+
+  if (scope & eSymbolContextBlock) {
+    Block *block_to_use = (block && use_inline_block_range)
+                              ? block->GetContainingInlinedBlock()
+                              : block;
+    if (block_to_use) {
+      Address addr;
+      block_to_use->GetStartAddress(addr);
+      return addr;
+    }
+  }
+
+  if ((scope & eSymbolContextFunction) && (function != nullptr))
+    return function->GetAddress();
+
+  if ((scope & eSymbolContextSymbol) && (symbol != nullptr))
+    return symbol->GetAddress();
+
+  return Address();
+}
+
 LanguageType SymbolContext::GetLanguage() const {
   LanguageType lang;
   if (function && (lang = function->GetLanguage()) != eLanguageTypeUnknown) {

>From 47f1c2624bc17fb22578484c0df8781701b003dd Mon Sep 17 00:00:00 2001
From: Pavel Labath <pavel at labath.sk>
Date: Fri, 17 Jan 2025 14:35:19 +0100
Subject: [PATCH 2/3] format

---
 lldb/source/Commands/CommandObjectTarget.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/lldb/source/Commands/CommandObjectTarget.cpp b/lldb/source/Commands/CommandObjectTarget.cpp
index 705c69167badd3..aef175880641f6 100644
--- a/lldb/source/Commands/CommandObjectTarget.cpp
+++ b/lldb/source/Commands/CommandObjectTarget.cpp
@@ -3566,7 +3566,8 @@ class CommandObjectTargetModulesShowUnwind : public CommandObjectParsed {
         continue;
       if (!sc.module_sp || sc.module_sp->GetObjectFile() == nullptr)
         continue;
-      Address addr = sc.GetAddress(eSymbolContextFunction | eSymbolContextSymbol, false);
+      Address addr =
+          sc.GetAddress(eSymbolContextFunction | eSymbolContextSymbol, false);
       if (!addr.IsValid())
         continue;
       ConstString funcname(sc.GetFunctionName());

>From 5ace1e6d87b56ae0c946596f7efdf9b9154cb693 Mon Sep 17 00:00:00 2001
From: Pavel Labath <pavel at labath.sk>
Date: Mon, 20 Jan 2025 13:10:44 +0100
Subject: [PATCH 3/3] add type name

---
 lldb/include/lldb/Symbol/SymbolContext.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lldb/include/lldb/Symbol/SymbolContext.h b/lldb/include/lldb/Symbol/SymbolContext.h
index fde11f275dc60a..f954f558841434 100644
--- a/lldb/include/lldb/Symbol/SymbolContext.h
+++ b/lldb/include/lldb/Symbol/SymbolContext.h
@@ -165,7 +165,7 @@ class SymbolContext {
   ///     eSymbolContextSymbol is set in \a scope
   ///
   /// \param[in] scope
-  ///     A mask of symbol context bits telling this function which
+  ///     A mask of \b SymbolContextItem bits telling this function which
   ///     address ranges it can use when trying to extract one from
   ///     the valid (non-nullptr) symbol context classes.
   ///



More information about the lldb-commits mailing list