From def07ea0df5cefe227955f6cdaae2c668dc84f0f Mon Sep 17 00:00:00 2001 From: Igor Kudrin Date: Thu, 2 Oct 2025 22:42:06 -0700 Subject: [PATCH 1/2] [lldb][test] Fix TestEmptyFuncThreadStepOut.py The test did not work as intended when the empty function 'done()' contained epilog code, because a breakpoint was set on the first instruction of the epilog instead of on the last instruction of the function. This caused the test to pass even with the fix from #126838 reverted. --- .../TestEmptyFuncThreadStepOut.py | 20 +++++++++++++++---- .../thread/finish-from-empty-func/main.c | 1 + 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lldb/test/API/functionalities/thread/finish-from-empty-func/TestEmptyFuncThreadStepOut.py b/lldb/test/API/functionalities/thread/finish-from-empty-func/TestEmptyFuncThreadStepOut.py index f5d3da530f4f5..be0d47d1d8fe6 100644 --- a/lldb/test/API/functionalities/thread/finish-from-empty-func/TestEmptyFuncThreadStepOut.py +++ b/lldb/test/API/functionalities/thread/finish-from-empty-func/TestEmptyFuncThreadStepOut.py @@ -15,10 +15,23 @@ class FinishFromEmptyFunctionTestCase(TestBase): def test_finish_from_empty_function(self): """Test that when stopped at a breakpoint in an empty function, finish leaves it correctly.""" self.build() - exe = self.getBuildArtifact("a.out") - target, process, thread, _ = lldbutil.run_to_name_breakpoint( - self, "done", exe_name=exe + target, _, thread, _ = lldbutil.run_to_source_breakpoint( + self, "// Set breakpoint here", lldb.SBFileSpec("main.c") ) + # Find the last instruction address of 'done()' and set a breakpoint there. + error = lldb.SBError() + ret_bp_addr = lldb.SBAddress() + while True: + thread.StepInstruction(False, error) + self.assertTrue(error.Success()) + frame = thread.GetSelectedFrame() + if "done" in frame.GetFunctionName(): + ret_bp_addr = frame.GetPCAddress() + elif ret_bp_addr.IsValid(): + break + ret_bp = target.BreakpointCreateByAddress(ret_bp_addr.GetLoadAddress(target)) + self.assertTrue(ret_bp.IsValid()) + self.runCmd("cont") if self.TraceOn(): self.runCmd("bt") @@ -29,7 +42,6 @@ def test_finish_from_empty_function(self): ) self.assertTrue(safety_bp.IsValid()) - error = lldb.SBError() thread.StepOut(error) self.assertTrue(error.Success()) diff --git a/lldb/test/API/functionalities/thread/finish-from-empty-func/main.c b/lldb/test/API/functionalities/thread/finish-from-empty-func/main.c index bc66a548a89df..b3f90db5e2562 100644 --- a/lldb/test/API/functionalities/thread/finish-from-empty-func/main.c +++ b/lldb/test/API/functionalities/thread/finish-from-empty-func/main.c @@ -2,6 +2,7 @@ void done() {} int main() { puts("in main"); + done(); // Set breakpoint here done(); puts("leaving main"); return 0; From 5ff7d663500d40a29b9988aa069784653f718178 Mon Sep 17 00:00:00 2001 From: Igor Kudrin Date: Thu, 16 Oct 2025 23:54:30 -0700 Subject: [PATCH 2/2] fixup! Add comments to the test --- .../TestEmptyFuncThreadStepOut.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lldb/test/API/functionalities/thread/finish-from-empty-func/TestEmptyFuncThreadStepOut.py b/lldb/test/API/functionalities/thread/finish-from-empty-func/TestEmptyFuncThreadStepOut.py index be0d47d1d8fe6..c95a57ff55815 100644 --- a/lldb/test/API/functionalities/thread/finish-from-empty-func/TestEmptyFuncThreadStepOut.py +++ b/lldb/test/API/functionalities/thread/finish-from-empty-func/TestEmptyFuncThreadStepOut.py @@ -13,12 +13,15 @@ class FinishFromEmptyFunctionTestCase(TestBase): @skipIf(compiler="clang", compiler_version=['<', '17.0']) def test_finish_from_empty_function(self): - """Test that when stopped at a breakpoint in an empty function, finish leaves it correctly.""" + """Test that when stopped at a breakpoint located at the last instruction + of a function, finish leaves it correctly.""" self.build() target, _, thread, _ = lldbutil.run_to_source_breakpoint( self, "// Set breakpoint here", lldb.SBFileSpec("main.c") ) - # Find the last instruction address of 'done()' and set a breakpoint there. + # Find the address of the last instruction of 'done()' and set a breakpoint there. + # Even though 'done()' is empty, it may contain prologue and epilogue code, so + # simply setting a breakpoint at the function can place it before 'ret'. error = lldb.SBError() ret_bp_addr = lldb.SBAddress() while True: @@ -28,9 +31,12 @@ def test_finish_from_empty_function(self): if "done" in frame.GetFunctionName(): ret_bp_addr = frame.GetPCAddress() elif ret_bp_addr.IsValid(): + # The entire function 'done()' has been stepped through, so 'ret_bp_addr' + # now contains the address of its last instruction, i.e. 'ret'. break ret_bp = target.BreakpointCreateByAddress(ret_bp_addr.GetLoadAddress(target)) self.assertTrue(ret_bp.IsValid()) + # Resume the execution and hit the new breakpoint. self.runCmd("cont") if self.TraceOn(): self.runCmd("bt")