forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 341
[lldb][Swift] Small steps towards fixing StepOut in async functions #9138
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
felipepiovezan
merged 3 commits into
swiftlang:stable/20240723
from
felipepiovezan:felipe/step_out_preliminaries
Aug 20, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
SWIFT_SOURCES := main.swift | ||
SWIFTFLAGS_EXTRAS := -parse-as-library | ||
include Makefile.rules |
44 changes: 44 additions & 0 deletions
44
lldb/test/API/lang/swift/async/stepping/step_out/TestSteppingOutAsync.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import lldb | ||
from lldbsuite.test.decorators import * | ||
import lldbsuite.test.lldbtest as lldbtest | ||
import lldbsuite.test.lldbutil as lldbutil | ||
import re | ||
|
||
|
||
class TestCase(lldbtest.TestBase): | ||
|
||
def check_and_get_frame_names(self, process): | ||
frames = process.GetSelectedThread().frames | ||
# Expected frames: | ||
# ASYNC___0___ <- ASYNC___1___ <- ASYNC___2___ <- ASYNC___3___ <- Main | ||
num_frames = 5 | ||
func_names = [frame.GetFunctionName() for frame in frames[:num_frames]] | ||
for idx in range(num_frames - 1): | ||
self.assertIn(f"ASYNC___{idx}___", func_names[idx]) | ||
self.assertIn("Main", func_names[num_frames - 1]) | ||
return func_names | ||
|
||
def step_out_checks(self, thread, expected_func_names): | ||
# Keep stepping out, comparing the top frame's name with the expected name. | ||
for expected_func_name in expected_func_names: | ||
error = lldb.SBError() | ||
thread.StepOut(error) | ||
self.assertSuccess(error, "step out failed") | ||
stop_reason = thread.GetStopReason() | ||
self.assertStopReason(stop_reason, lldb.eStopReasonPlanComplete) | ||
self.assertEqual(thread.frames[0].GetFunctionName(), expected_func_name) | ||
|
||
@swiftTest | ||
@skipIf(oslist=["windows", "linux"]) | ||
@skipIf("rdar://133849022") | ||
def test(self): | ||
"""Test `frame variable` in async functions""" | ||
self.build() | ||
|
||
source_file = lldb.SBFileSpec("main.swift") | ||
target, process, thread, bkpt = lldbutil.run_to_source_breakpoint( | ||
self, "BREAK HERE", source_file | ||
) | ||
|
||
func_names = self.check_and_get_frame_names(process) | ||
self.step_out_checks(thread, func_names[1:]) |
38 changes: 38 additions & 0 deletions
38
lldb/test/API/lang/swift/async/stepping/step_out/main.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
func work() { | ||
print("working") | ||
} | ||
|
||
func ASYNC___0___() async -> Int { | ||
var myvar = 111; | ||
work() | ||
myvar += 1; // BREAK HERE | ||
return myvar | ||
} | ||
|
||
func ASYNC___1___() async -> Int { | ||
var blah = 333 | ||
work() | ||
var result = await ASYNC___0___() | ||
work() | ||
return result + blah | ||
} | ||
|
||
func ASYNC___2___() async -> Int { | ||
work() | ||
var result1 = await ASYNC___1___() | ||
work() | ||
return result1 | ||
} | ||
|
||
func ASYNC___3___() async -> Int { | ||
var result = await ASYNC___2___() | ||
work() | ||
return result | ||
} | ||
|
||
@main struct Main { | ||
static func main() async { | ||
let result = await ASYNC___3___() | ||
print(result) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
add_lldb_unittest(StackIDTests | ||
StackIDTest.cpp | ||
|
||
LINK_LIBS | ||
lldbCore | ||
lldbTarget | ||
lldbPluginPlatformMacOSX | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
|
||
#include "lldb/Target/StackID.h" | ||
#include "Plugins/Platform/MacOSX/PlatformMacOSX.h" | ||
#include "Plugins/Platform/MacOSX/PlatformRemoteMacOSX.h" | ||
#include "lldb/Core/Debugger.h" | ||
#include "lldb/Host/HostInfo.h" | ||
#include "lldb/Target/Process.h" | ||
#include "gtest/gtest.h" | ||
|
||
using namespace lldb_private; | ||
using namespace lldb; | ||
|
||
static std::once_flag initialize_flag; | ||
|
||
// Initialize the bare minimum to enable defining a mock Process class. | ||
class StackIDTest : public ::testing::Test { | ||
public: | ||
void SetUp() override { | ||
std::call_once(initialize_flag, []() { | ||
HostInfo::Initialize(); | ||
PlatformMacOSX::Initialize(); | ||
FileSystem::Initialize(); | ||
}); | ||
ArchSpec arch("x86_64-apple-macosx-"); | ||
Platform::SetHostPlatform( | ||
PlatformRemoteMacOSX::CreateInstance(true, &arch)); | ||
m_debugger_sp = Debugger::CreateInstance(); | ||
m_debugger_sp->GetTargetList().CreateTarget(*m_debugger_sp, "", arch, | ||
eLoadDependentsNo, | ||
m_platform_sp, m_target_sp); | ||
ASSERT_TRUE(m_target_sp); | ||
ASSERT_TRUE(m_target_sp->GetArchitecture().IsValid()); | ||
ASSERT_TRUE(m_platform_sp); | ||
} | ||
|
||
PlatformSP m_platform_sp; | ||
TargetSP m_target_sp; | ||
DebuggerSP m_debugger_sp; | ||
}; | ||
|
||
struct MockProcess : Process { | ||
MockProcess(TargetSP target_sp, ListenerSP listener_sp) | ||
: Process(target_sp, listener_sp) {} | ||
size_t DoReadMemory(addr_t vm_addr, void *buf, size_t size, | ||
Status &error) override { | ||
return 0; | ||
} | ||
size_t ReadMemory(addr_t addr, void *buf, size_t size, | ||
Status &status) override { | ||
return DoReadMemory(addr, buf, size, status); | ||
} | ||
bool CanDebug(TargetSP, bool) override { return true; } | ||
Status DoDestroy() override { return Status(); } | ||
llvm::StringRef GetPluginName() override { return ""; } | ||
void RefreshStateAfterStop() override {} | ||
bool DoUpdateThreadList(ThreadList &, ThreadList &) override { return false; } | ||
}; | ||
|
||
enum OnStack { Yes, No }; | ||
/// Helper class to enable testing StackID::IsYounger. | ||
struct MockStackID : StackID { | ||
MockStackID(addr_t cfa, OnStack on_stack) : StackID() { | ||
SetPC(0); | ||
SetCFA(cfa); | ||
m_cfa_on_stack = on_stack == OnStack::Yes ? LazyBool::eLazyBoolYes | ||
: LazyBool::eLazyBoolNo; | ||
} | ||
}; | ||
|
||
TEST_F(StackIDTest, StackStackCFAComparison) { | ||
auto process = MockProcess(m_target_sp, Listener::MakeListener("dummy")); | ||
|
||
MockStackID small_cfa_on_stack(/*cfa*/ 10, OnStack::Yes); | ||
MockStackID big_cfa_on_stack(/*cfa*/ 100, OnStack::Yes); | ||
|
||
EXPECT_TRUE( | ||
StackID::IsYounger(small_cfa_on_stack, big_cfa_on_stack, process)); | ||
EXPECT_FALSE( | ||
StackID::IsYounger(big_cfa_on_stack, small_cfa_on_stack, process)); | ||
} | ||
|
||
TEST_F(StackIDTest, StackHeapCFAComparison) { | ||
auto process = MockProcess(m_target_sp, Listener::MakeListener("dummy")); | ||
|
||
MockStackID cfa_on_stack(/*cfa*/ 100, OnStack::Yes); | ||
MockStackID cfa_on_heap(/*cfa*/ 10, OnStack::No); | ||
|
||
EXPECT_TRUE(StackID::IsYounger(cfa_on_stack, cfa_on_heap, process)); | ||
|
||
// FIXME: if the above returned true, swapping the arguments **must** return | ||
// false. And yet it doesn't. | ||
// EXPECT_FALSE(StackID::IsYounger(cfa_on_heap, cfa_on_stack, process)); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given the PR description:
should this be an expectedFailure?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was debating this but we would risk having sporadic xpasses: if the stars align and the "heap cfas" numeric values are such that certain StackIDs comparisons behave in some pattern, the test could pass.