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
15#include "lldb/Utility/Log.h"
16
17using namespace lldb;
18using namespace lldb_private;
19using namespace lldb_private::formatters;
20
22 if (!valobj_sp)
24
25 // We expect a single pointer in the `coroutine_handle` class.
26 // We don't care about its name.
27 if (valobj_sp->GetNumChildrenIgnoringErrors() != 1)
29 ValueObjectSP ptr_sp(valobj_sp->GetChildAtIndex(0));
30 if (!ptr_sp)
32 if (!ptr_sp->GetCompilerType().IsPointerType())
34
35 AddressType addr_type;
36 lldb::addr_t frame_ptr_addr = ptr_sp->GetPointerValue(&addr_type);
37 if (!frame_ptr_addr || frame_ptr_addr == LLDB_INVALID_ADDRESS)
39 lldbassert(addr_type == AddressType::eAddressTypeLoad);
40 if (addr_type != AddressType::eAddressTypeLoad)
42
43 return frame_ptr_addr;
44}
45
47 lldb::addr_t frame_ptr_addr) {
48 lldb::ProcessSP process_sp = target_sp->GetProcessSP();
49 auto ptr_size = process_sp->GetAddressByteSize();
50
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 return destroy_func_address.CalculateSymbolContextFunction();
63}
64
65static CompilerType InferPromiseType(Function &destroy_func) {
66 Block &block = destroy_func.GetBlock(true);
67 auto variable_list = block.GetBlockVariableList(true);
68
69 // clang generates an artificial `__promise` variable inside the
70 // `destroy` function. Look for it.
71 auto promise_var = variable_list->FindVariable(ConstString("__promise"));
72 if (!promise_var)
73 return {};
74 if (!promise_var->IsArtificial())
75 return {};
76
77 Type *promise_type = promise_var->GetType();
78 if (!promise_type)
79 return {};
80 return promise_type->GetForwardCompilerType();
81}
82
84 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
85 lldb::addr_t frame_ptr_addr =
87 if (frame_ptr_addr == LLDB_INVALID_ADDRESS)
88 return false;
89
90 if (frame_ptr_addr == 0) {
91 stream << "nullptr";
92 } else {
93 stream.Printf("coro frame = 0x%" PRIx64, frame_ptr_addr);
94 }
95
96 return true;
97}
98
101 : SyntheticChildrenFrontEnd(*valobj_sp) {
102 if (valobj_sp)
103 Update();
104}
105
108
109llvm::Expected<uint32_t> lldb_private::formatters::
111 if (!m_resume_ptr_sp || !m_destroy_ptr_sp)
112 return 0;
113
114 return m_promise_ptr_sp ? 3 : 2;
115}
116
119 switch (idx) {
120 case 0:
121 return m_resume_ptr_sp;
122 case 1:
123 return m_destroy_ptr_sp;
124 case 2:
125 return m_promise_ptr_sp;
126 }
127 return lldb::ValueObjectSP();
128}
129
132 m_resume_ptr_sp.reset();
133 m_destroy_ptr_sp.reset();
134 m_promise_ptr_sp.reset();
135
136 ValueObjectSP valobj_sp = m_backend.GetNonSyntheticValue();
137 if (!valobj_sp)
139
140 lldb::addr_t frame_ptr_addr = GetCoroFramePtrFromHandle(valobj_sp);
141 if (frame_ptr_addr == 0 || frame_ptr_addr == LLDB_INVALID_ADDRESS)
143
144 auto ts = valobj_sp->GetCompilerType().GetTypeSystem();
145 auto ast_ctx = ts.dyn_cast_or_null<TypeSystemClang>();
146 if (!ast_ctx)
148
149 // Create the `resume` and `destroy` children.
150 lldb::TargetSP target_sp = m_backend.GetTargetSP();
151 auto &exe_ctx = m_backend.GetExecutionContextRef();
152 lldb::ProcessSP process_sp = target_sp->GetProcessSP();
153 auto ptr_size = process_sp->GetAddressByteSize();
154 CompilerType void_type = ast_ctx->GetBasicType(lldb::eBasicTypeVoid);
155 CompilerType coro_func_type = ast_ctx->CreateFunctionType(
156 /*result_type=*/void_type, /*args=*/&void_type, /*num_args=*/1,
157 /*is_variadic=*/false, /*qualifiers=*/0);
158 CompilerType coro_func_ptr_type = coro_func_type.GetPointerType();
159 m_resume_ptr_sp = CreateValueObjectFromAddress(
160 "resume", frame_ptr_addr + 0 * ptr_size, exe_ctx, coro_func_ptr_type);
161 lldbassert(m_resume_ptr_sp);
162 m_destroy_ptr_sp = CreateValueObjectFromAddress(
163 "destroy", frame_ptr_addr + 1 * ptr_size, exe_ctx, coro_func_ptr_type);
164 lldbassert(m_destroy_ptr_sp);
165
166 // Get the `promise_type` from the template argument
167 CompilerType promise_type(
168 valobj_sp->GetCompilerType().GetTypeTemplateArgument(0));
169 if (!promise_type)
171
172 // Try to infer the promise_type if it was type-erased
173 if (promise_type.IsVoidType()) {
174 if (Function *destroy_func =
175 ExtractDestroyFunction(target_sp, frame_ptr_addr)) {
176 if (CompilerType inferred_type = InferPromiseType(*destroy_func)) {
177 promise_type = inferred_type;
178 }
179 }
180 }
181
182 // If we don't know the promise type, we don't display the `promise` member.
183 // `CreateValueObjectFromAddress` below would fail for `void` types.
184 if (promise_type.IsVoidType()) {
186 }
187
188 // Add the `promise` member. We intentionally add `promise` as a pointer type
189 // instead of a value type, and don't automatically dereference this pointer.
190 // We do so to avoid potential very deep recursion in case there is a cycle
191 // formed between `std::coroutine_handle`s and their promises.
192 lldb::ValueObjectSP promise = CreateValueObjectFromAddress(
193 "promise", frame_ptr_addr + 2 * ptr_size, exe_ctx, promise_type);
195 lldb::ValueObjectSP promisePtr = promise->AddressOf(error);
196 if (error.Success())
197 m_promise_ptr_sp = promisePtr->Clone(ConstString("promise"));
198
200}
201
204 return true;
205}
206
208 ConstString name) {
210 return UINT32_MAX;
211
212 if (name == ConstString("resume"))
213 return 0;
214 if (name == ConstString("destroy"))
215 return 1;
216 if (name == ConstString("promise_ptr") && m_promise_ptr_sp)
217 return 2;
218
219 return UINT32_MAX;
220}
221
225 return (valobj_sp ? new StdlibCoroutineHandleSyntheticFrontEnd(valobj_sp)
226 : nullptr);
227}
static llvm::raw_ostream & error(Stream &strm)
static Function * ExtractDestroyFunction(lldb::TargetSP target_sp, lldb::addr_t frame_ptr_addr)
Definition: Coroutines.cpp:46
static lldb::addr_t GetCoroFramePtrFromHandle(ValueObjectSP valobj_sp)
Definition: Coroutines.cpp:21
static CompilerType InferPromiseType(Function &destroy_func)
Definition: Coroutines.cpp:65
#define lldbassert(x)
Definition: LLDBAssert.h:15
A section + offset based address class.
Definition: Address.h:62
Function * CalculateSymbolContextFunction() const
Definition: Address.cpp:872
A class that describes a single lexical block.
Definition: Block.h:41
lldb::VariableListSP GetBlockVariableList(bool can_create)
Get the variable list for this block only.
Definition: Block.cpp:415
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
CompilerType GetPointerType() const
Return a new CompilerType that is a pointer to this type.
A uniqued constant string class.
Definition: ConstString.h:40
A class that describes a function.
Definition: Function.h:399
Block & GetBlock(bool can_create)
Get accessor for the block list.
Definition: Function.cpp:371
An error handling class.
Definition: Status.h:44
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:134
A TypeSystem implementation based on Clang.
CompilerType GetForwardCompilerType()
Definition: Type.cpp:754
virtual lldb::ValueObjectSP GetNonSyntheticValue()
Definition: ValueObject.h:606
Synthetic children frontend for std::coroutine_handle<promise_type> from libc++, libstdc++ and MSVC S...
Definition: Coroutines.h:31
lldb::ChildCacheState Update() override
This function is assumed to always succeed and if it fails, the front-end should know to deal with it...
Definition: Coroutines.cpp:131
llvm::Expected< uint32_t > CalculateNumChildren() override
Definition: Coroutines.cpp:110
lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override
Definition: Coroutines.cpp:118
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:82
#define UINT32_MAX
Definition: lldb-defines.h:19
SyntheticChildrenFrontEnd * StdlibCoroutineHandleSyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP)
Definition: Coroutines.cpp:223
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:83
A class that represents a running process on the host machine.
Definition: SBAddress.h:15
ChildCacheState
Specifies if children need to be re-computed after a call to SyntheticChildrenFrontEnd::Update.
@ eRefetch
Children need to be recomputed dynamically.
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
Definition: lldb-forward.h:480
std::shared_ptr< lldb_private::Process > ProcessSP
Definition: lldb-forward.h:387
uint64_t addr_t
Definition: lldb-types.h:80
std::shared_ptr< lldb_private::Target > TargetSP
Definition: lldb-forward.h:444