[Lldb-commits] [lldb] [lldb] Allow languages to filter breakpoints set by line (PR #83908)

Felipe de Azevedo Piovezan via lldb-commits lldb-commits at lists.llvm.org
Tue Mar 5 18:49:44 PST 2024


https://github.com/felipepiovezan updated https://github.com/llvm/llvm-project/pull/83908

>From 51307b548d09c34ee06037ccf459110e451970a9 Mon Sep 17 00:00:00 2001
From: Felipe de Azevedo Piovezan <fpiovezan at apple.com>
Date: Mon, 4 Mar 2024 09:56:18 -0800
Subject: [PATCH 1/2] [lldb] Allow languages to filter breakpoints set by line

Some languages may create artificial functions that have no real user code, even
though there is line table information for them. One such case is with coroutine
code that receives the CoroSplitter transformation in LLVM IR. That code
transformation creates many different Functions, cloning one Instruction into
many Instructions in many different Functions and copying the associated debug
locations.

It would be difficult to make that pass delete debug locations of cloned
instructions in a language agnostic way (is it even possible?), but LLDB can
ignore certain locations by querying its Language APIs and having it decide
based on, for example, mangling information.
---
 lldb/include/lldb/Target/Language.h           |  4 ++++
 lldb/source/Breakpoint/BreakpointResolver.cpp | 12 ++++++++++++
 2 files changed, 16 insertions(+)

diff --git a/lldb/include/lldb/Target/Language.h b/lldb/include/lldb/Target/Language.h
index 0cbd8a32dccd54..957c40eb7c0772 100644
--- a/lldb/include/lldb/Target/Language.h
+++ b/lldb/include/lldb/Target/Language.h
@@ -339,6 +339,10 @@ class Language : public PluginInterface {
 
   virtual llvm::StringRef GetInstanceVariableName() { return {}; }
 
+  virtual bool IsInterestingCtxForLineBreakpoint(const SymbolContext &) const {
+    return true;
+  }
+
 protected:
   // Classes that inherit from Language can see and modify these
 
diff --git a/lldb/source/Breakpoint/BreakpointResolver.cpp b/lldb/source/Breakpoint/BreakpointResolver.cpp
index bc6348716ef418..876b30c6d76d55 100644
--- a/lldb/source/Breakpoint/BreakpointResolver.cpp
+++ b/lldb/source/Breakpoint/BreakpointResolver.cpp
@@ -23,6 +23,7 @@
 #include "lldb/Symbol/CompileUnit.h"
 #include "lldb/Symbol/Function.h"
 #include "lldb/Symbol/SymbolContext.h"
+#include "lldb/Target/Language.h"
 #include "lldb/Target/Target.h"
 #include "lldb/Utility/LLDBLog.h"
 #include "lldb/Utility/Log.h"
@@ -199,6 +200,15 @@ bool operator<(const SourceLoc lhs, const SourceLoc rhs) {
 }
 } // namespace
 
+static void
+ApplyLanguageFilters(llvm::SmallVectorImpl<SymbolContext> &sc_list) {
+  llvm::erase_if(sc_list, [](SymbolContext &sc) {
+    if (Language *lang = Language::FindPlugin(sc.GetLanguage()))
+      return !lang->IsInterestingCtxForLineBreakpoint(sc);
+    return false;
+  });
+}
+
 void BreakpointResolver::SetSCMatchesByLine(
     SearchFilter &filter, SymbolContextList &sc_list, bool skip_prologue,
     llvm::StringRef log_ident, uint32_t line, std::optional<uint16_t> column) {
@@ -206,6 +216,8 @@ void BreakpointResolver::SetSCMatchesByLine(
   for (uint32_t i = 0; i < sc_list.GetSize(); ++i)
     all_scs.push_back(sc_list[i]);
 
+  ApplyLanguageFilters(all_scs);
+
   while (all_scs.size()) {
     uint32_t closest_line = UINT32_MAX;
 

>From 7ea76d6c04d063d50da52756179639f2037aa793 Mon Sep 17 00:00:00 2001
From: Felipe de Azevedo Piovezan <fpiovezan at apple.com>
Date: Tue, 5 Mar 2024 18:47:51 -0800
Subject: [PATCH 2/2] fixup! add target option

---
 lldb/include/lldb/Target/Language.h           |  9 ++++++--
 lldb/include/lldb/Target/Target.h             |  2 ++
 lldb/source/Breakpoint/BreakpointResolver.cpp | 23 +++++++++----------
 lldb/source/Target/Target.cpp                 |  8 +++++++
 lldb/source/Target/TargetProperties.td        |  3 +++
 5 files changed, 31 insertions(+), 14 deletions(-)

diff --git a/lldb/include/lldb/Target/Language.h b/lldb/include/lldb/Target/Language.h
index 957c40eb7c0772..9db9f4e297e114 100644
--- a/lldb/include/lldb/Target/Language.h
+++ b/lldb/include/lldb/Target/Language.h
@@ -339,8 +339,13 @@ class Language : public PluginInterface {
 
   virtual llvm::StringRef GetInstanceVariableName() { return {}; }
 
-  virtual bool IsInterestingCtxForLineBreakpoint(const SymbolContext &) const {
-    return true;
+  // Returns true if this SymbolContext should be used when setting breakpoints
+  // by line (number or regex). This is useful for languages that create
+  // artificial functions without any meaningful user code associated with them
+  // (e.g. code that gets expanded in late compilation stages, like by
+  // CoroSplitter).
+  virtual bool IsArtificialCtxForLineBreakpoint(const SymbolContext &) const {
+    return false;
   }
 
 protected:
diff --git a/lldb/include/lldb/Target/Target.h b/lldb/include/lldb/Target/Target.h
index 8f57358981d4d2..ab04aa57f17300 100644
--- a/lldb/include/lldb/Target/Target.h
+++ b/lldb/include/lldb/Target/Target.h
@@ -258,6 +258,8 @@ class TargetProperties : public Properties {
 
   bool GetDebugUtilityExpression() const;
 
+  bool GetIgnoreBreakpointsFromLanguageArtificialLocations() const;
+
 private:
   // Callbacks for m_launch_info.
   void Arg0ValueChangedCallback();
diff --git a/lldb/source/Breakpoint/BreakpointResolver.cpp b/lldb/source/Breakpoint/BreakpointResolver.cpp
index 876b30c6d76d55..03f10f876ee1db 100644
--- a/lldb/source/Breakpoint/BreakpointResolver.cpp
+++ b/lldb/source/Breakpoint/BreakpointResolver.cpp
@@ -200,23 +200,22 @@ bool operator<(const SourceLoc lhs, const SourceLoc rhs) {
 }
 } // namespace
 
-static void
-ApplyLanguageFilters(llvm::SmallVectorImpl<SymbolContext> &sc_list) {
-  llvm::erase_if(sc_list, [](SymbolContext &sc) {
-    if (Language *lang = Language::FindPlugin(sc.GetLanguage()))
-      return !lang->IsInterestingCtxForLineBreakpoint(sc);
-    return false;
-  });
-}
-
 void BreakpointResolver::SetSCMatchesByLine(
     SearchFilter &filter, SymbolContextList &sc_list, bool skip_prologue,
     llvm::StringRef log_ident, uint32_t line, std::optional<uint16_t> column) {
   llvm::SmallVector<SymbolContext, 16> all_scs;
-  for (uint32_t i = 0; i < sc_list.GetSize(); ++i)
+  const bool ShouldQueryLanguageFilter =
+      GetBreakpoint()
+          ->GetTarget()
+          .GetIgnoreBreakpointsFromLanguageArtificialLocations();
+
+  for (uint32_t i = 0; i < sc_list.GetSize(); ++i) {
+    if (ShouldQueryLanguageFilter)
+      if (Language *lang = Language::FindPlugin(sc_list[i].GetLanguage());
+          lang && lang->IsArtificialCtxForLineBreakpoint(sc_list[i]))
+        continue;
     all_scs.push_back(sc_list[i]);
-
-  ApplyLanguageFilters(all_scs);
+  }
 
   while (all_scs.size()) {
     uint32_t closest_line = UINT32_MAX;
diff --git a/lldb/source/Target/Target.cpp b/lldb/source/Target/Target.cpp
index e982a30a3ae4ff..ba533ad440ff19 100644
--- a/lldb/source/Target/Target.cpp
+++ b/lldb/source/Target/Target.cpp
@@ -4896,6 +4896,14 @@ void TargetProperties::SetDebugUtilityExpression(bool debug) {
   SetPropertyAtIndex(idx, debug);
 }
 
+bool TargetProperties::GetIgnoreBreakpointsFromLanguageArtificialLocations()
+    const {
+  const uint32_t idx =
+      ePropertyIgnoreBreakpointsFromLanguageArtificialLocations;
+  return GetPropertyAtIndexAs<bool>(
+      idx, g_target_properties[idx].default_uint_value != 0);
+}
+
 // Target::TargetEventData
 
 Target::TargetEventData::TargetEventData(const lldb::TargetSP &target_sp)
diff --git a/lldb/source/Target/TargetProperties.td b/lldb/source/Target/TargetProperties.td
index d2fccdb7b9b39c..6151d2e6524a86 100644
--- a/lldb/source/Target/TargetProperties.td
+++ b/lldb/source/Target/TargetProperties.td
@@ -104,6 +104,9 @@ let Definition = "target" in {
   def BreakpointUseAvoidList: Property<"breakpoints-use-platform-avoid-list", "Boolean">,
     DefaultTrue,
     Desc<"Consult the platform module avoid list when setting non-module specific breakpoints.">;
+  def IgnoreBreakpointsFromLanguageArtificialLocations: Property<"ignore-breakpoints-from-language-artificial-locations", "Boolean">,
+    DefaultTrue,
+    Desc<"If true, locations that are considered artificial by the source Language plugin will be ignored when setting breakpoints by line number or regex.">;
   def Arg0: Property<"arg0", "String">,
     DefaultStringValue<"">,
     Desc<"The first argument passed to the program in the argument array which can be different from the executable itself.">;



More information about the lldb-commits mailing list