LLDB  mainline
Coroutines.cpp
Go to the documentation of this file.
1 //===-- Coroutines.cpp ----------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "Coroutines.h"
10 
13 #include "lldb/Symbol/Function.h"
15 
16 using namespace lldb;
17 using namespace lldb_private;
18 using namespace lldb_private::formatters;
19 
20 static ValueObjectSP GetCoroFramePtrFromHandle(ValueObject &valobj) {
21  ValueObjectSP valobj_sp(valobj.GetNonSyntheticValue());
22  if (!valobj_sp)
23  return nullptr;
24 
25  // We expect a single pointer in the `coroutine_handle` class.
26  // We don't care about its name.
27  if (valobj_sp->GetNumChildren() != 1)
28  return nullptr;
29  ValueObjectSP ptr_sp(valobj_sp->GetChildAtIndex(0, true));
30  if (!ptr_sp)
31  return nullptr;
32  if (!ptr_sp->GetCompilerType().IsPointerType())
33  return nullptr;
34 
35  return ptr_sp;
36 }
37 
38 static Function *ExtractDestroyFunction(ValueObjectSP &frame_ptr_sp) {
39  lldb::TargetSP target_sp = frame_ptr_sp->GetTargetSP();
40  lldb::ProcessSP process_sp = frame_ptr_sp->GetProcessSP();
41  auto ptr_size = process_sp->GetAddressByteSize();
42 
43  AddressType addr_type;
44  lldb::addr_t frame_ptr_addr = frame_ptr_sp->GetPointerValue(&addr_type);
45  if (!frame_ptr_addr || frame_ptr_addr == LLDB_INVALID_ADDRESS)
46  return nullptr;
48 
49  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);
55  if (error.Fail())
56  return nullptr;
57 
58  Address destroy_func_address;
59  if (!target_sp->ResolveLoadAddress(destroy_func_addr, destroy_func_address))
60  return nullptr;
61 
62  Function *destroy_func =
63  destroy_func_address.CalculateSymbolContextFunction();
64  if (!destroy_func)
65  return nullptr;
66 
67  return destroy_func;
68 }
69 
70 static CompilerType InferPromiseType(Function &destroy_func) {
71  Block &block = destroy_func.GetBlock(true);
72  auto variable_list = block.GetBlockVariableList(true);
73 
74  // clang generates an artificial `__promise` variable inside the
75  // `destroy` function. Look for it.
76  auto promise_var = variable_list->FindVariable(ConstString("__promise"));
77  if (!promise_var)
78  return {};
79  if (!promise_var->IsArtificial())
80  return {};
81 
82  Type *promise_type = promise_var->GetType();
83  if (!promise_type)
84  return {};
85  return promise_type->GetForwardCompilerType();
86 }
87 
89  CompilerType promise_type) {
90  CompilerType void_type = ast_ctx.GetBasicType(lldb::eBasicTypeVoid);
91  CompilerType coro_func_type = ast_ctx.CreateFunctionType(
92  /*result_type=*/void_type, /*args=*/&void_type, /*num_args=*/1,
93  /*is_variadic=*/false, /*qualifiers=*/0);
94  CompilerType coro_abi_type;
95  if (promise_type.IsVoidType()) {
96  coro_abi_type = ast_ctx.CreateStructForIdentifier(
97  ConstString(), {{"resume", coro_func_type.GetPointerType()},
98  {"destroy", coro_func_type.GetPointerType()}});
99  } else {
100  coro_abi_type = ast_ctx.CreateStructForIdentifier(
101  ConstString(), {{"resume", coro_func_type.GetPointerType()},
102  {"destroy", coro_func_type.GetPointerType()},
103  {"promise", promise_type}});
104  }
105  return coro_abi_type;
106 }
107 
109  ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
110  ValueObjectSP ptr_sp(GetCoroFramePtrFromHandle(valobj));
111  if (!ptr_sp)
112  return false;
113 
114  if (!ptr_sp->GetValueAsUnsigned(0)) {
115  stream << "nullptr";
116  } else {
117  stream.Printf("coro frame = 0x%" PRIx64, ptr_sp->GetValueAsUnsigned(0));
118  }
119  return true;
120 }
121 
123  StdlibCoroutineHandleSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
124  : SyntheticChildrenFrontEnd(*valobj_sp),
125  m_ast_importer(std::make_unique<ClangASTImporter>()) {
126  if (valobj_sp)
127  Update();
128 }
129 
132 
135  if (!m_frame_ptr_sp)
136  return 0;
137 
138  return m_frame_ptr_sp->GetNumChildren();
139 }
140 
141 lldb::ValueObjectSP lldb_private::formatters::
143  if (!m_frame_ptr_sp)
144  return lldb::ValueObjectSP();
145 
146  return m_frame_ptr_sp->GetChildAtIndex(idx, true);
147 }
148 
151  m_frame_ptr_sp.reset();
152 
153  ValueObjectSP valobj_sp = m_backend.GetSP();
154  if (!valobj_sp)
155  return false;
156 
157  ValueObjectSP ptr_sp(GetCoroFramePtrFromHandle(m_backend));
158  if (!ptr_sp)
159  return false;
160 
161  // Get the `promise_type` from the template argument
162  CompilerType promise_type(
163  valobj_sp->GetCompilerType().GetTypeTemplateArgument(0));
164  if (!promise_type)
165  return false;
166 
167  // Try to infer the promise_type if it was type-erased
168  auto ts = valobj_sp->GetCompilerType().GetTypeSystem();
169  auto ast_ctx = ts.dyn_cast_or_null<TypeSystemClang>();
170  if (!ast_ctx)
171  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  }
178  }
179  }
180 
181  // Build the coroutine frame type
182  CompilerType coro_frame_type = GetCoroutineFrameType(*ast_ctx, promise_type);
183 
184  m_frame_ptr_sp = ptr_sp->Cast(coro_frame_type.GetPointerType());
185 
186  return false;
187 }
188 
191  return true;
192 }
193 
195  ConstString name) {
196  if (!m_frame_ptr_sp)
197  return UINT32_MAX;
198 
199  return m_frame_ptr_sp->GetIndexOfChildWithName(name);
200 }
201 
204  CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
205  return (valobj_sp ? new StdlibCoroutineHandleSyntheticFrontEnd(valobj_sp)
206  : nullptr);
207 }
lldb_private::formatters::StdlibCoroutineHandleSyntheticFrontEnd::StdlibCoroutineHandleSyntheticFrontEnd
StdlibCoroutineHandleSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
Definition: Coroutines.cpp:123
lldb_private::formatters::StdlibCoroutineHandleSyntheticFrontEnd::~StdlibCoroutineHandleSyntheticFrontEnd
~StdlibCoroutineHandleSyntheticFrontEnd() override
lldb_private::Block::GetBlockVariableList
lldb::VariableListSP GetBlockVariableList(bool can_create)
Get the variable list for this block only.
Definition: Block.cpp:399
Coroutines.h
lldb_private::Address::CalculateSymbolContextFunction
Function * CalculateSymbolContextFunction() const
Definition: Address.cpp:865
lldb_private::formatters::StdlibCoroutineHandleSyntheticFrontEnd::GetChildAtIndex
lldb::ValueObjectSP GetChildAtIndex(size_t idx) override
Definition: Coroutines.cpp:142
lldb_private::AddressType
AddressType
Definition: lldb-private-enumerations.h:30
lldb_private::TypeSystemClang::CreateFunctionType
CompilerType CreateFunctionType(const CompilerType &result_type, const CompilerType *args, unsigned num_args, bool is_variadic, unsigned type_quals, clang::CallingConv cc=clang::CC_C, clang::RefQualifierKind ref_qual=clang::RQ_None)
Definition: TypeSystemClang.cpp:2212
lldb_private::SyntheticChildrenFrontEnd
Definition: TypeSynthetic.h:27
lldb_private::Stream
Definition: Stream.h:28
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
lldb_private::formatters::StdlibCoroutineHandleSyntheticFrontEnd::GetIndexOfChildWithName
size_t GetIndexOfChildWithName(ConstString name) override
Definition: Coroutines.cpp:194
lldb_private::TypeSystemClang::CreateStructForIdentifier
CompilerType CreateStructForIdentifier(ConstString type_name, const std::initializer_list< std::pair< const char *, CompilerType >> &type_fields, bool packed=false)
Definition: TypeSystemClang.cpp:2309
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::Function
Definition: Function.h:409
lldb_private::formatters::StdlibCoroutineHandleSyntheticFrontEnd::Update
bool Update() override
Definition: Coroutines.cpp:150
TypeSystemClang.h
lldb_private::Function::GetBlock
Block & GetBlock(bool can_create)
Get accessor for the block list.
Definition: Function.cpp:345
lldb_private::ValueObject::GetNonSyntheticValue
virtual lldb::ValueObjectSP GetNonSyntheticValue()
Definition: ValueObject.h:592
lldb_private::formatters::StdlibCoroutineHandleSummaryProvider
bool StdlibCoroutineHandleSummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
Summary provider for std::coroutine_handle<T> from libc++, libstdc++ and MSVC STL.
Definition: Coroutines.cpp:108
lldb_private::ConstString
Definition: ConstString.h:39
lldb_private::CompilerType::IsVoidType
bool IsVoidType() const
Definition: CompilerType.cpp:278
lldb_private::formatters::StdlibCoroutineHandleSyntheticFrontEndCreator
SyntheticChildrenFrontEnd * StdlibCoroutineHandleSyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP)
Definition: Coroutines.cpp:203
lldb::eBasicTypeVoid
@ eBasicTypeVoid
Definition: lldb-enumerations.h:756
InferPromiseType
static CompilerType InferPromiseType(Function &destroy_func)
Definition: Coroutines.cpp:70
lldb_private::eAddressTypeLoad
@ eAddressTypeLoad
Address is an address as in the current target inferior process.
Definition: lldb-private-enumerations.h:34
lldb_private::formatters::StdlibCoroutineHandleSyntheticFrontEnd::CalculateNumChildren
size_t CalculateNumChildren() override
Definition: Coroutines.cpp:134
VariableList.h
GetCoroFramePtrFromHandle
static ValueObjectSP GetCoroFramePtrFromHandle(ValueObject &valobj)
Definition: Coroutines.cpp:20
lldb_private::formatters::StdlibCoroutineHandleSyntheticFrontEnd
Synthetic children frontend for std::coroutine_handle<promise_type> from libc++, libstdc++ and MSVC S...
Definition: Coroutines.h:32
lldbassert
#define lldbassert(x)
Definition: LLDBAssert.h:15
lldb_private::Status
Definition: Status.h:44
lldb_private::ValueObject
ValueObject:
Definition: ValueObject.h:105
lldb_private::CompilerType::GetPointerType
CompilerType GetPointerType() const
Return a new CompilerType that is a pointer to this type.
Definition: CompilerType.cpp:474
lldb_private::Block
Definition: Block.h:41
ExtractDestroyFunction
static Function * ExtractDestroyFunction(ValueObjectSP &frame_ptr_sp)
Definition: Coroutines.cpp:38
lldb_private::Address
Definition: Address.h:59
lldb_private::Type
Definition: Type.h:68
lldb_private::Type::GetForwardCompilerType
CompilerType GetForwardCompilerType()
Definition: Type.cpp:667
UINT32_MAX
#define UINT32_MAX
Definition: lldb-defines.h:19
lldb_private::TypeSummaryOptions
Definition: TypeSummary.h:26
lldb_private::ClangASTImporter
Manages and observes all Clang AST node importing in LLDB.
Definition: ClangASTImporter.h:64
lldb_private::formatters::StdlibCoroutineHandleSyntheticFrontEnd::m_frame_ptr_sp
lldb::ValueObjectSP m_frame_ptr_sp
Definition: Coroutines.h:50
LLDB_INVALID_ADDRESS
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:74
lldb_private::CompilerType
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
Function.h
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
lldb_private::formatters
Definition: CXXFunctionPointer.h:15
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::TypeSystemClang::GetBasicType
CompilerType GetBasicType(lldb::BasicType type)
Definition: TypeSystemClang.cpp:933
lldb_private::TypeSystemClang
A TypeSystem implementation based on Clang.
Definition: TypeSystemClang.h:106
ClangASTImporter.h
lldb_private::CXXSyntheticChildren
Definition: TypeSynthetic.h:358
GetCoroutineFrameType
static CompilerType GetCoroutineFrameType(TypeSystemClang &ast_ctx, CompilerType promise_type)
Definition: Coroutines.cpp:88
lldb
Definition: SBAddress.h:15
lldb_private::formatters::StdlibCoroutineHandleSyntheticFrontEnd::MightHaveChildren
bool MightHaveChildren() override
Definition: Coroutines.cpp:190