Skip to content

[lldb] Use Address to setup breakpoint #94794

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 2 commits into from
Jan 6, 2025

Conversation

yln
Copy link
Collaborator

@yln yln commented Jun 7, 2024

Use Address (instead of addr_t) to setup
breakpoint in ReportRetriever::SetupBreakpoint.
This is cleaner and the breakpoint should now
survive re-running of the binary.

rdar://124399066

@yln yln requested review from jimingham and usama54321 June 7, 2024 19:44
@yln yln self-assigned this Jun 7, 2024
@yln yln requested a review from JDevlieghere as a code owner June 7, 2024 19:44
@llvmbot llvmbot added the lldb label Jun 7, 2024
@llvmbot
Copy link
Member

llvmbot commented Jun 7, 2024

@llvm/pr-subscribers-lldb

Author: Julian Lettner (yln)

Changes

Use Address (instead of addr_t) to setup
breakpoint in ReportRetriever::SetupBreakpoint.
This is cleaner and the breakpoint should now
survive re-running of the binary.

rdar://124399066


Full diff: https://github.com/llvm/llvm-project/pull/94794.diff

2 Files Affected:

  • (modified) lldb/source/Plugins/InstrumentationRuntime/ASanLibsanitizers/InstrumentationRuntimeASanLibsanitizers.cpp (+2-10)
  • (modified) lldb/source/Plugins/InstrumentationRuntime/Utility/ReportRetriever.cpp (+2-8)
diff --git a/lldb/source/Plugins/InstrumentationRuntime/ASanLibsanitizers/InstrumentationRuntimeASanLibsanitizers.cpp b/lldb/source/Plugins/InstrumentationRuntime/ASanLibsanitizers/InstrumentationRuntimeASanLibsanitizers.cpp
index cd91f4a6ff1bc..b1151febb7cc4 100644
--- a/lldb/source/Plugins/InstrumentationRuntime/ASanLibsanitizers/InstrumentationRuntimeASanLibsanitizers.cpp
+++ b/lldb/source/Plugins/InstrumentationRuntime/ASanLibsanitizers/InstrumentationRuntimeASanLibsanitizers.cpp
@@ -90,17 +90,9 @@ void InstrumentationRuntimeASanLibsanitizers::Activate() {
   if (!process_sp)
     return;
 
-  lldb::ModuleSP module_sp = GetRuntimeModuleSP();
-
   Breakpoint *breakpoint = ReportRetriever::SetupBreakpoint(
-      module_sp, process_sp, ConstString("sanitizers_address_on_report"));
-
-  if (!breakpoint) {
-    breakpoint = ReportRetriever::SetupBreakpoint(
-        module_sp, process_sp,
-        ConstString("_Z22raise_sanitizers_error23sanitizer_error_context"));
-  }
-
+      GetRuntimeModuleSP(), process_sp,
+      ConstString("sanitizers_address_on_report"));
   if (!breakpoint)
     return;
 
diff --git a/lldb/source/Plugins/InstrumentationRuntime/Utility/ReportRetriever.cpp b/lldb/source/Plugins/InstrumentationRuntime/Utility/ReportRetriever.cpp
index 298b63bc716fc..4e382005f8c2b 100644
--- a/lldb/source/Plugins/InstrumentationRuntime/Utility/ReportRetriever.cpp
+++ b/lldb/source/Plugins/InstrumentationRuntime/Utility/ReportRetriever.cpp
@@ -219,7 +219,6 @@ bool ReportRetriever::NotifyBreakpointHit(ProcessSP process_sp,
   return true; // Return true to stop the target
 }
 
-// FIXME: Setup the breakpoint using a less fragile SPI. rdar://124399066
 Breakpoint *ReportRetriever::SetupBreakpoint(ModuleSP module_sp,
                                              ProcessSP process_sp,
                                              ConstString symbol_name) {
@@ -235,18 +234,13 @@ Breakpoint *ReportRetriever::SetupBreakpoint(ModuleSP module_sp,
   if (!symbol->ValueIsAddress() || !symbol->GetAddressRef().IsValid())
     return nullptr;
 
-  Target &target = process_sp->GetTarget();
-  addr_t symbol_address = symbol->GetAddressRef().GetOpcodeLoadAddress(&target);
-
-  if (symbol_address == LLDB_INVALID_ADDRESS)
-    return nullptr;
-
+  const Address &address = symbol->GetAddressRef();
   const bool internal = true;
   const bool hardware = false;
 
   Breakpoint *breakpoint =
       process_sp->GetTarget()
-          .CreateBreakpoint(symbol_address, internal, hardware)
+          .CreateBreakpoint(address, internal, hardware)
           .get();
 
   return breakpoint;

const bool internal = true;
const bool hardware = false;

Breakpoint *breakpoint =
process_sp->GetTarget()
.CreateBreakpoint(symbol_address, internal, hardware)
.CreateBreakpoint(address, internal, hardware)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleanup suggested by @jimingham here: #84583 (comment)

you could set it by Address, in which case it would survive re-running, but instead it converts the Address to an addr_t - the most fragile way to set a breakpoint

So we are using the CreateBreakpoint overload that takes Address instead of addr_t now and Jim mentioned that this survives re-running. Are we concerned at all about this change in behavior? For example, could we get into a situation where we keep adding superfluous breakpoints because the old ones survive?
@jimingham @usama54321

if (!breakpoint) {
breakpoint = ReportRetriever::SetupBreakpoint(
module_sp, process_sp,
ConstString("_Z22raise_sanitizers_error23sanitizer_error_context"));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@usama54321 it's now safe to ignore the legacy symbol, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that should be fine

yln added 2 commits June 7, 2024 12:55
Use `Address` (instead of `addr_t`) to setup
breakpoint in `ReportRetriever::SetupBreakpoint`.
This is cleaner and the breakpoint should now
survive re-running of the binary.

rdar://124399066
@yln yln force-pushed the users/yln/lldb-ReportRetriever_SetupBreakpoint-cleanup branch from dd02ca5 to 484f913 Compare June 7, 2024 19:55
@llvm llvm deleted a comment from github-actions bot Jun 7, 2024
@DavidSpickett
Copy link
Collaborator

I'm assuming that Address keeps track of the value if it changes due to position independent code, ASLR, etc. between runs. So this seems fine.

Can this be tested? Not sure what coverage we have of these plugins right now.

Comment on lines +94 to +95
GetRuntimeModuleSP(), process_sp,
ConstString("sanitizers_address_on_report"));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look right. Is this left over from the older code? Remove this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @clayborg, I don't follow. Can you elaborate?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@clayborg The old code had a fallback to _Z22raise_sanitizers_error23sanitizer_error_context but this breakpoint is still the right one.

Copy link
Member

@JDevlieghere JDevlieghere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment on lines +94 to +95
GetRuntimeModuleSP(), process_sp,
ConstString("sanitizers_address_on_report"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@clayborg The old code had a fallback to _Z22raise_sanitizers_error23sanitizer_error_context but this breakpoint is still the right one.

Copy link
Collaborator

@jimingham jimingham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. It was silly to remove the section information from the Address only to have the Breakpoint have to find it again.

The only potentially concerning change here is the removal of a check for when you go to set the breakpoint and the Address you got for the Symbol was not yet loaded into the process you are debugging (at removed lines 241-2).

You could easily put that check back in but that was not necessary in the original code, since you only setup the breakpoint after you see the RuntimeModule get loaded; and it's doubly unnecessary now since you switched to setting the breakpoint with an Address gotten from a module, so it will track the correct load address even if set before the load.

@yln yln merged commit 01e980e into main Jan 6, 2025
6 checks passed
@yln yln deleted the users/yln/lldb-ReportRetriever_SetupBreakpoint-cleanup branch January 6, 2025 23:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants