[Lldb-commits] [lldb] 4346318 - [LLDB] Recognize `std::noop_coroutine()` in `std::coroutine_handle` pretty printer

Adrian Vogelsgesang via lldb-commits lldb-commits at lists.llvm.org
Sun Nov 20 11:19:07 PST 2022


Author: Adrian Vogelsgesang
Date: 2022-11-20T11:18:52-08:00
New Revision: 4346318f5c700f4e85f866610fb8328fc429319b

URL: https://github.com/llvm/llvm-project/commit/4346318f5c700f4e85f866610fb8328fc429319b
DIFF: https://github.com/llvm/llvm-project/commit/4346318f5c700f4e85f866610fb8328fc429319b.diff

LOG: [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

Added: 
    

Modified: 
    lldb/source/Plugins/Language/CPlusPlus/Coroutines.cpp
    lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/coroutine_handle/TestCoroutineHandle.py
    lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/coroutine_handle/main.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/source/Plugins/Language/CPlusPlus/Coroutines.cpp b/lldb/source/Plugins/Language/CPlusPlus/Coroutines.cpp
index bd9ff99db67b8..aa6a6ef7e56ae 100644
--- a/lldb/source/Plugins/Language/CPlusPlus/Coroutines.cpp
+++ b/lldb/source/Plugins/Language/CPlusPlus/Coroutines.cpp
@@ -35,7 +35,7 @@ static ValueObjectSP GetCoroFramePtrFromHandle(ValueObject &valobj) {
   return ptr_sp;
 }
 
-static Function *ExtractDestroyFunction(ValueObjectSP &frame_ptr_sp) {
+static Function *ExtractFunction(ValueObjectSP &frame_ptr_sp, int offset) {
   lldb::TargetSP target_sp = frame_ptr_sp->GetTargetSP();
   lldb::ProcessSP process_sp = frame_ptr_sp->GetProcessSP();
   auto ptr_size = process_sp->GetAddressByteSize();
@@ -47,24 +47,64 @@ static Function *ExtractDestroyFunction(ValueObjectSP &frame_ptr_sp) {
   lldbassert(addr_type == AddressType::eAddressTypeLoad);
 
   Status error;
-  // The destroy pointer is the 2nd pointer inside the compiler-generated
-  // `pair<resumePtr,destroyPtr>`.
-  auto destroy_func_ptr_addr = frame_ptr_addr + ptr_size;
-  lldb::addr_t destroy_func_addr =
-      process_sp->ReadPointerFromMemory(destroy_func_ptr_addr, error);
+  auto func_ptr_addr = frame_ptr_addr + offset * ptr_size;
+  lldb::addr_t func_addr =
+      process_sp->ReadPointerFromMemory(func_ptr_addr, error);
   if (error.Fail())
     return nullptr;
 
-  Address destroy_func_address;
-  if (!target_sp->ResolveLoadAddress(destroy_func_addr, destroy_func_address))
+  Address func_address;
+  if (!target_sp->ResolveLoadAddress(func_addr, func_address))
     return nullptr;
 
-  Function *destroy_func =
-      destroy_func_address.CalculateSymbolContextFunction();
-  if (!destroy_func)
-    return nullptr;
+  return func_address.CalculateSymbolContextFunction();
+}
+
+static Function *ExtractResumeFunction(ValueObjectSP &frame_ptr_sp) {
+  return ExtractFunction(frame_ptr_sp, 0);
+}
+
+static Function *ExtractDestroyFunction(ValueObjectSP &frame_ptr_sp) {
+  return ExtractFunction(frame_ptr_sp, 1);
+}
+
+static bool IsNoopCoroFunction(Function *f) {
+  if (!f)
+    return false;
 
-  return destroy_func;
+  // clang's `__builtin_coro_noop` gets lowered to
+  // `_NoopCoro_ResumeDestroy`. This is used by libc++
+  // on clang.
+  auto mangledName = f->GetMangled().GetMangledName();
+  if (mangledName == "__NoopCoro_ResumeDestroy")
+    return true;
+
+  // libc++ uses the following name as a fallback on
+  // compilers without `__builtin_coro_noop`.
+  auto name = f->GetNameNoArguments();
+  static RegularExpression libcxxRegex(
+      "^std::coroutine_handle<std::noop_coroutine_promise>::"
+      "__noop_coroutine_frame_ty_::__dummy_resume_destroy_func$");
+  lldbassert(libcxxRegex.IsValid());
+  if (libcxxRegex.Execute(name.GetStringRef()))
+    return true;
+  static RegularExpression libcxxRegexAbiNS(
+      "^std::__[[:alnum:]]+::coroutine_handle<std::__[[:alnum:]]+::"
+      "noop_coroutine_promise>::__noop_coroutine_frame_ty_::"
+      "__dummy_resume_destroy_func$");
+  lldbassert(libcxxRegexAbiNS.IsValid());
+  if (libcxxRegexAbiNS.Execute(name.GetStringRef()))
+    return true;
+
+  // libstdc++ uses the following name on both gcc and clang.
+  static RegularExpression libstdcppRegex(
+      "^std::__[[:alnum:]]+::coroutine_handle<std::__[[:alnum:]]+::"
+      "noop_coroutine_promise>::__frame::__dummy_resume_destroy$");
+  lldbassert(libstdcppRegex.IsValid());
+  if (libstdcppRegex.Execute(name.GetStringRef()))
+    return true;
+
+  return false;
 }
 
 static CompilerType InferPromiseType(Function &destroy_func) {
@@ -113,9 +153,15 @@ bool lldb_private::formatters::StdlibCoroutineHandleSummaryProvider(
 
   if (!ptr_sp->GetValueAsUnsigned(0)) {
     stream << "nullptr";
-  } else {
-    stream.Printf("coro frame = 0x%" PRIx64, ptr_sp->GetValueAsUnsigned(0));
+    return true;
   }
+  if (IsNoopCoroFunction(ExtractResumeFunction(ptr_sp)) &&
+      IsNoopCoroFunction(ExtractDestroyFunction(ptr_sp))) {
+    stream << "noop_coroutine";
+    return true;
+  }
+
+  stream.Printf("coro frame = 0x%" PRIx64, ptr_sp->GetValueAsUnsigned(0));
   return true;
 }
 
@@ -158,6 +204,14 @@ bool lldb_private::formatters::StdlibCoroutineHandleSyntheticFrontEnd::
   if (!ptr_sp)
     return false;
 
+  Function *resume_func = ExtractResumeFunction(ptr_sp);
+  Function *destroy_func = ExtractDestroyFunction(ptr_sp);
+
+  if (IsNoopCoroFunction(resume_func) && IsNoopCoroFunction(destroy_func)) {
+    // For `std::noop_coroutine()`, we don't want to display any child nodes.
+    return false;
+  }
+
   // Get the `promise_type` from the template argument
   CompilerType promise_type(
       valobj_sp->GetCompilerType().GetTypeTemplateArgument(0));
@@ -169,12 +223,10 @@ bool lldb_private::formatters::StdlibCoroutineHandleSyntheticFrontEnd::
   auto ast_ctx = ts.dyn_cast_or_null<TypeSystemClang>();
   if (!ast_ctx)
     return false;
-  if (promise_type.IsVoidType()) {
-    if (Function *destroy_func = ExtractDestroyFunction(ptr_sp)) {
-      if (CompilerType inferred_type = InferPromiseType(*destroy_func)) {
-        // Copy the type over to the correct `TypeSystemClang` instance
-        promise_type = m_ast_importer->CopyType(*ast_ctx, inferred_type);
-      }
+  if (promise_type.IsVoidType() && destroy_func) {
+    if (CompilerType inferred_type = InferPromiseType(*destroy_func)) {
+      // Copy the type over to the correct `TypeSystemClang` instance
+      promise_type = m_ast_importer->CopyType(*ast_ctx, inferred_type);
     }
   }
 

diff  --git a/lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/coroutine_handle/TestCoroutineHandle.py b/lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/coroutine_handle/TestCoroutineHandle.py
index 44e5e6451c10d..d1b4d59de5f5f 100644
--- a/lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/coroutine_handle/TestCoroutineHandle.py
+++ b/lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/coroutine_handle/TestCoroutineHandle.py
@@ -38,6 +38,13 @@ def do_test(self, stdlib_type):
                     ValueCheck(name="current_value", value = "-1"),
                 ])
             ])
+        # We recognize and pretty-print `std::noop_coroutine`. We don't display
+        # any children as those are irrelevant for the noop coroutine.
+        # clang version < 16 did not yet write debug info for the noop coroutines.
+        if not (is_clang and self.expectedCompilerVersion(["<", "16"])):
+            self.expect_expr("noop_hdl",
+                result_summary="noop_coroutine",
+                result_children=[])
         if is_clang:
             # For a type-erased `coroutine_handle<>`, we can still devirtualize
             # the promise call and display the correctly typed promise.

diff  --git a/lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/coroutine_handle/main.cpp b/lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/coroutine_handle/main.cpp
index 8cb81c3bc9f4c..e34637f09f6ed 100644
--- a/lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/coroutine_handle/main.cpp
+++ b/lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/coroutine_handle/main.cpp
@@ -45,6 +45,7 @@ int main() {
   std::coroutine_handle<> type_erased_hdl = gen.hdl;
   std::coroutine_handle<int> incorrectly_typed_hdl =
       std::coroutine_handle<int>::from_address(gen.hdl.address());
+  std::coroutine_handle<> noop_hdl = std::noop_coroutine();
   gen.hdl.resume();                            // Break at initial_suspend
   gen.hdl.resume();                            // Break after co_yield
   empty_function_so_we_can_set_a_breakpoint(); // Break at final_suspend


        


More information about the lldb-commits mailing list