Skip to content

Commit 9158af0

Browse files
[lldb][swift] Filter Q funclets when setting breakpoints
These funclets only serve to `task_dealloc` previously allocated tasks when returning from an async call, and immediately `task_switch` to the next await-suspend funclet (which contains real user code). By not filtering out these funclets, any breakpoint on a line with an async call will cause execution to pause 3 times: once before the call, twice when "returning" from the call, which makes for a confusing experience. The patch does the filtering on `BreakpointResolver::SetSCMatchesByLine`, which is the common code between BreakpointResolverFileLine and BreakpointResolverFileRegex. We also considered changing the debug line information in any of the many different lowering stages the swift compiler, but this turned out to be very complex to do in a targeted way; more often than not, a handful of early-IR coroutine instructions get expanded into multiple function clones, all inheriting the same debug line information. The current approach also has the advantaged of being easily reversible if we decide to do so.
1 parent bb8b8f0 commit 9158af0

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

lldb/source/Plugins/Language/Swift/SwiftLanguage.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,6 +1814,17 @@ SwiftLanguage::GetDemangledFunctionNameWithoutArguments(Mangled mangled) const {
18141814
return mangled_name;
18151815
}
18161816

1817+
bool SwiftLanguage::IgnoreForLineBreakpoints(const SymbolContext &sc) const {
1818+
// If we don't have a function, conservatively return false.
1819+
if (!sc.function)
1820+
return false;
1821+
StringRef name = sc.function->GetMangled().GetMangledName().GetStringRef();
1822+
// In async functions, ignore await resume ("Q") funclets, these only
1823+
// deallocate the async context and task_switch back to user code.
1824+
return SwiftLanguageRuntime::IsSwiftAsyncAwaitResumePartialFunctionSymbol(
1825+
name);
1826+
}
1827+
18171828
//------------------------------------------------------------------
18181829
// Static Functions
18191830
//------------------------------------------------------------------

lldb/source/Plugins/Language/Swift/SwiftLanguage.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ class SwiftLanguage : public Language {
9090

9191
llvm::StringRef GetInstanceVariableName() override { return "self"; }
9292

93+
/// Override that skips breakpoints inside await resume ("Q") async funclets.
94+
bool IgnoreForLineBreakpoints(const SymbolContext &sc) const override;
95+
9396
//------------------------------------------------------------------
9497
// PluginInterface protocol
9598
//------------------------------------------------------------------
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SWIFT_SOURCES := main.swift
2+
3+
include Makefile.rules
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import lldb
2+
from lldbsuite.test.decorators import *
3+
import lldbsuite.test.lldbtest as lldbtest
4+
import lldbsuite.test.lldbutil as lldbutil
5+
6+
7+
class TestSwiftAsyncBreakpoints(lldbtest.TestBase):
8+
@swiftTest
9+
@skipIfWindows
10+
@skipIfLinux
11+
@skipIf(archs=no_match(["arm64", "arm64e", "x86_64"]))
12+
def test(self):
13+
"""Test async breakpoints"""
14+
self.build()
15+
filespec = lldb.SBFileSpec("main.swift")
16+
target, process, thread, breakpoint1 = lldbutil.run_to_source_breakpoint(
17+
self, "Breakpoint1", filespec
18+
)
19+
breakpoint2 = target.BreakpointCreateBySourceRegex("Breakpoint2", filespec)
20+
breakpoint3 = target.BreakpointCreateBySourceRegex("Breakpoint3", filespec)
21+
self.assertEquals(breakpoint1.GetNumLocations(), 2)
22+
self.assertEquals(breakpoint2.GetNumLocations(), 1)
23+
self.assertEquals(breakpoint3.GetNumLocations(), 2)
24+
25+
location11 = breakpoint1.GetLocationAtIndex(0)
26+
location12 = breakpoint1.GetLocationAtIndex(1)
27+
self.assertEquals(location11.GetHitCount(), 1)
28+
self.assertEquals(location12.GetHitCount(), 0)
29+
30+
self.assertEquals(thread.GetStopDescription(128), "breakpoint 1.1")
31+
process.Continue()
32+
self.assertEquals(thread.GetStopDescription(128), "breakpoint 1.2")
33+
34+
thread.StepOver()
35+
self.assertEquals(thread.GetStopDescription(128), "breakpoint 2.1")
36+
self.expect("expr timestamp1", substrs=["42"])
37+
38+
thread.StepOver()
39+
self.assertIn("breakpoint 3.1", thread.GetStopDescription(128))
40+
self.expect("expr timestamp1", substrs=["42"])
41+
42+
process.Continue()
43+
self.assertIn("breakpoint 3.2", thread.GetStopDescription(128))
44+
self.expect("expr timestamp1", substrs=["42"])
45+
46+
thread.StepOver()
47+
self.expect("expr timestamp1", substrs=["42"])
48+
self.expect("expr timestamp2", substrs=["43"])
49+
50+
self.runCmd("settings set language.enable-filter-for-line-breakpoints false")
51+
breakpoint1_no_filter = target.BreakpointCreateBySourceRegex(
52+
"Breakpoint1", filespec
53+
)
54+
self.assertEquals(breakpoint1_no_filter.GetNumLocations(), 3)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func getTimestamp(i:Int) async -> Int {
2+
return i
3+
}
4+
5+
func work() {}
6+
7+
func foo() async {
8+
work()
9+
let timestamp1 = await getTimestamp(i:42) // Breakpoint1
10+
work() // Breakpoint2
11+
let timestamp2 = await getTimestamp(i:43) // Breakpoint3
12+
work()
13+
}
14+
15+
await foo()

0 commit comments

Comments
 (0)