Skip to content

Commit 4346318

Browse files
committed
[LLDB] Recognize std::noop_coroutine() in std::coroutine_handle pretty printer
With this commit, the `std::coroutine_handle` pretty printer now recognizes `std::noop_coroutine()` handles. For noop coroutine handles, we identify use the summary string `noop_coroutine` and we don't print children Instead of ``` (std::coroutine_handle<void>) $3 = coro frame = 0x555555559058 { resume = 0x00005555555564f0 (a.out`std::__1::coroutine_handle<std::__1::noop_coroutine_promise>::__noop_coroutine_frame_ty_::__dummy_resume_destroy_func() at noop_coroutine_handle.h:79) destroy = 0x00005555555564f0 (a.out`std::__1::coroutine_handle<std::__1::noop_coroutine_promise>::__noop_coroutine_frame_ty_::__dummy_resume_destroy_func() at noop_coroutine_handle.h:79) } ``` we now print ``` (std::coroutine_handle<void>) $3 = noop_coroutine ``` Differential Revision: https://reviews.llvm.org/D132735
1 parent 20ca119 commit 4346318

File tree

3 files changed

+81
-21
lines changed

3 files changed

+81
-21
lines changed

lldb/source/Plugins/Language/CPlusPlus/Coroutines.cpp

Lines changed: 73 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static ValueObjectSP GetCoroFramePtrFromHandle(ValueObject &valobj) {
3535
return ptr_sp;
3636
}
3737

38-
static Function *ExtractDestroyFunction(ValueObjectSP &frame_ptr_sp) {
38+
static Function *ExtractFunction(ValueObjectSP &frame_ptr_sp, int offset) {
3939
lldb::TargetSP target_sp = frame_ptr_sp->GetTargetSP();
4040
lldb::ProcessSP process_sp = frame_ptr_sp->GetProcessSP();
4141
auto ptr_size = process_sp->GetAddressByteSize();
@@ -47,24 +47,64 @@ static Function *ExtractDestroyFunction(ValueObjectSP &frame_ptr_sp) {
4747
lldbassert(addr_type == AddressType::eAddressTypeLoad);
4848

4949
Status error;
50-
// The destroy pointer is the 2nd pointer inside the compiler-generated
51-
// `pair<resumePtr,destroyPtr>`.
52-
auto destroy_func_ptr_addr = frame_ptr_addr + ptr_size;
53-
lldb::addr_t destroy_func_addr =
54-
process_sp->ReadPointerFromMemory(destroy_func_ptr_addr, error);
50+
auto func_ptr_addr = frame_ptr_addr + offset * ptr_size;
51+
lldb::addr_t func_addr =
52+
process_sp->ReadPointerFromMemory(func_ptr_addr, error);
5553
if (error.Fail())
5654
return nullptr;
5755

58-
Address destroy_func_address;
59-
if (!target_sp->ResolveLoadAddress(destroy_func_addr, destroy_func_address))
56+
Address func_address;
57+
if (!target_sp->ResolveLoadAddress(func_addr, func_address))
6058
return nullptr;
6159

62-
Function *destroy_func =
63-
destroy_func_address.CalculateSymbolContextFunction();
64-
if (!destroy_func)
65-
return nullptr;
60+
return func_address.CalculateSymbolContextFunction();
61+
}
62+
63+
static Function *ExtractResumeFunction(ValueObjectSP &frame_ptr_sp) {
64+
return ExtractFunction(frame_ptr_sp, 0);
65+
}
66+
67+
static Function *ExtractDestroyFunction(ValueObjectSP &frame_ptr_sp) {
68+
return ExtractFunction(frame_ptr_sp, 1);
69+
}
70+
71+
static bool IsNoopCoroFunction(Function *f) {
72+
if (!f)
73+
return false;
6674

67-
return destroy_func;
75+
// clang's `__builtin_coro_noop` gets lowered to
76+
// `_NoopCoro_ResumeDestroy`. This is used by libc++
77+
// on clang.
78+
auto mangledName = f->GetMangled().GetMangledName();
79+
if (mangledName == "__NoopCoro_ResumeDestroy")
80+
return true;
81+
82+
// libc++ uses the following name as a fallback on
83+
// compilers without `__builtin_coro_noop`.
84+
auto name = f->GetNameNoArguments();
85+
static RegularExpression libcxxRegex(
86+
"^std::coroutine_handle<std::noop_coroutine_promise>::"
87+
"__noop_coroutine_frame_ty_::__dummy_resume_destroy_func$");
88+
lldbassert(libcxxRegex.IsValid());
89+
if (libcxxRegex.Execute(name.GetStringRef()))
90+
return true;
91+
static RegularExpression libcxxRegexAbiNS(
92+
"^std::__[[:alnum:]]+::coroutine_handle<std::__[[:alnum:]]+::"
93+
"noop_coroutine_promise>::__noop_coroutine_frame_ty_::"
94+
"__dummy_resume_destroy_func$");
95+
lldbassert(libcxxRegexAbiNS.IsValid());
96+
if (libcxxRegexAbiNS.Execute(name.GetStringRef()))
97+
return true;
98+
99+
// libstdc++ uses the following name on both gcc and clang.
100+
static RegularExpression libstdcppRegex(
101+
"^std::__[[:alnum:]]+::coroutine_handle<std::__[[:alnum:]]+::"
102+
"noop_coroutine_promise>::__frame::__dummy_resume_destroy$");
103+
lldbassert(libstdcppRegex.IsValid());
104+
if (libstdcppRegex.Execute(name.GetStringRef()))
105+
return true;
106+
107+
return false;
68108
}
69109

70110
static CompilerType InferPromiseType(Function &destroy_func) {
@@ -113,9 +153,15 @@ bool lldb_private::formatters::StdlibCoroutineHandleSummaryProvider(
113153

114154
if (!ptr_sp->GetValueAsUnsigned(0)) {
115155
stream << "nullptr";
116-
} else {
117-
stream.Printf("coro frame = 0x%" PRIx64, ptr_sp->GetValueAsUnsigned(0));
156+
return true;
118157
}
158+
if (IsNoopCoroFunction(ExtractResumeFunction(ptr_sp)) &&
159+
IsNoopCoroFunction(ExtractDestroyFunction(ptr_sp))) {
160+
stream << "noop_coroutine";
161+
return true;
162+
}
163+
164+
stream.Printf("coro frame = 0x%" PRIx64, ptr_sp->GetValueAsUnsigned(0));
119165
return true;
120166
}
121167

@@ -158,6 +204,14 @@ bool lldb_private::formatters::StdlibCoroutineHandleSyntheticFrontEnd::
158204
if (!ptr_sp)
159205
return false;
160206

207+
Function *resume_func = ExtractResumeFunction(ptr_sp);
208+
Function *destroy_func = ExtractDestroyFunction(ptr_sp);
209+
210+
if (IsNoopCoroFunction(resume_func) && IsNoopCoroFunction(destroy_func)) {
211+
// For `std::noop_coroutine()`, we don't want to display any child nodes.
212+
return false;
213+
}
214+
161215
// Get the `promise_type` from the template argument
162216
CompilerType promise_type(
163217
valobj_sp->GetCompilerType().GetTypeTemplateArgument(0));
@@ -169,12 +223,10 @@ bool lldb_private::formatters::StdlibCoroutineHandleSyntheticFrontEnd::
169223
auto ast_ctx = ts.dyn_cast_or_null<TypeSystemClang>();
170224
if (!ast_ctx)
171225
return false;
172-
if (promise_type.IsVoidType()) {
173-
if (Function *destroy_func = ExtractDestroyFunction(ptr_sp)) {
174-
if (CompilerType inferred_type = InferPromiseType(*destroy_func)) {
175-
// Copy the type over to the correct `TypeSystemClang` instance
176-
promise_type = m_ast_importer->CopyType(*ast_ctx, inferred_type);
177-
}
226+
if (promise_type.IsVoidType() && destroy_func) {
227+
if (CompilerType inferred_type = InferPromiseType(*destroy_func)) {
228+
// Copy the type over to the correct `TypeSystemClang` instance
229+
promise_type = m_ast_importer->CopyType(*ast_ctx, inferred_type);
178230
}
179231
}
180232

lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/coroutine_handle/TestCoroutineHandle.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ def do_test(self, stdlib_type):
3838
ValueCheck(name="current_value", value = "-1"),
3939
])
4040
])
41+
# We recognize and pretty-print `std::noop_coroutine`. We don't display
42+
# any children as those are irrelevant for the noop coroutine.
43+
# clang version < 16 did not yet write debug info for the noop coroutines.
44+
if not (is_clang and self.expectedCompilerVersion(["<", "16"])):
45+
self.expect_expr("noop_hdl",
46+
result_summary="noop_coroutine",
47+
result_children=[])
4148
if is_clang:
4249
# For a type-erased `coroutine_handle<>`, we can still devirtualize
4350
# the promise call and display the correctly typed promise.

lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/coroutine_handle/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ int main() {
4545
std::coroutine_handle<> type_erased_hdl = gen.hdl;
4646
std::coroutine_handle<int> incorrectly_typed_hdl =
4747
std::coroutine_handle<int>::from_address(gen.hdl.address());
48+
std::coroutine_handle<> noop_hdl = std::noop_coroutine();
4849
gen.hdl.resume(); // Break at initial_suspend
4950
gen.hdl.resume(); // Break after co_yield
5051
empty_function_so_we_can_set_a_breakpoint(); // Break at final_suspend

0 commit comments

Comments
 (0)