LLDB mainline
LanguageRuntime.h
Go to the documentation of this file.
1//===-- LanguageRuntime.h ---------------------------------------------------*-
2// C++ -*-===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLDB_TARGET_LANGUAGERUNTIME_H
11#define LLDB_TARGET_LANGUAGERUNTIME_H
12
16#include "lldb/Core/Value.h"
20#include "lldb/Target/Runtime.h"
21#include "lldb/Target/StackID.h"
23#include "lldb/lldb-private.h"
24#include "lldb/lldb-public.h"
25#include <optional>
26
27namespace lldb_private {
28
30public:
32 lldb::LanguageType language,
33 bool update_module_list = true);
34
35 ~ExceptionSearchFilter() override = default;
36
37 bool ModulePasses(const lldb::ModuleSP &module_sp) override;
38
39 bool ModulePasses(const FileSpec &spec) override;
40
41 void Search(Searcher &searcher) override;
42
43 void GetDescription(Stream *s) override;
44
45 static SearchFilter *
47 const StructuredData::Dictionary &data_dict,
48 Status &error);
49
51
52protected:
56
57 std::unique_ptr<SearchFilter> DoCreateCopy() override;
58
60};
61
62class LanguageRuntime : public Runtime, public PluginInterface {
63public:
64 static LanguageRuntime *FindPlugin(Process *process,
65 lldb::LanguageType language);
66
67 static void InitializeCommands(CommandObject *parent);
68
70
71 /// Return the preferred language runtime instance, which in most cases will
72 /// be the current instance.
74 return nullptr;
75 }
76
77 /// Returns whether this runtime is the preferred runtime for \p in_value, an
78 /// object of another language, for example because code in this runtime's
79 /// language uses it. The runtime of the object's language can then return
80 /// this runtime from GetPreferredLanguageRuntime().
82 return false;
83 }
84
85 virtual llvm::Error GetObjectDescription(Stream &str,
86 ValueObject &object) = 0;
87
88 virtual llvm::Error
90 ExecutionContextScope *exe_scope) = 0;
91
92 struct VTableInfo {
93 Address addr; /// Address of the vtable's virtual function table
94 Symbol *symbol; /// The vtable symbol from the symbol table
95 };
96 /// Get the vtable information for a given value.
97 ///
98 /// \param[in] in_value
99 /// The value object to try and extract the VTableInfo from.
100 ///
101 /// \param[in] check_type
102 /// If true, the compiler type of \a in_value will be checked to see if
103 /// it is an instance to, or pointer or reference to a class or struct
104 /// that has a vtable. If the type doesn't meet the requirements, an
105 /// error will be returned explaining why the type isn't suitable.
106 ///
107 /// \return
108 /// An error if anything goes wrong while trying to extract the vtable
109 /// or if \a check_type is true and the type doesn't have a vtable.
110 virtual llvm::Expected<VTableInfo> GetVTableInfo(ValueObject &in_value,
111 bool check_type) {
112 return llvm::createStringError(
113 std::errc::invalid_argument,
114 "language doesn't support getting vtable information");
115 }
116
117 /// This call should return true if it could set the name and/or the type
118 /// Sets address to the address of the dynamic type if value_type is set to
119 /// a file or load address. Sets local_buffer to a buffer containing the data
120 /// of the dynamic type if value_type is set to a host address. Callers should
121 /// copy local_buffer over into their own buffer if they want to keep the data
122 /// alive.
124 ValueObject &in_value, lldb::DynamicValueType use_dynamic,
125 TypeAndOrName &class_type_or_name, Address &address,
126 Value::ValueType &value_type, llvm::ArrayRef<uint8_t> &local_buffer) = 0;
127
128 // This call should return a CompilerType given a generic type name and an
129 // ExecutionContextScope in which one can actually fetch any specialization
130 // information required.
132 llvm::StringRef abstract_type_name) {
133 return CompilerType();
134 }
135
136 // This should be a fast test to determine whether it is likely that this
137 // value would have a dynamic type.
138 virtual bool CouldHaveDynamicValue(ValueObject &in_value) = 0;
139
140 // The contract for GetDynamicTypeAndAddress() is to return a "bare-bones"
141 // dynamic type For instance, given a Base* pointer,
142 // GetDynamicTypeAndAddress() will return the type of Derived, not Derived*.
143 // The job of this API is to correct this misalignment between the static
144 // type and the discovered dynamic type
145 virtual TypeAndOrName FixUpDynamicType(const TypeAndOrName &type_and_or_name,
146 ValueObject &static_value) = 0;
147
148 /// Returns the address of the instance that \p pointer refers to, where
149 /// \p pointer is the value of an object of \p type, whose instances are
150 /// pointers (see eTypeInstanceIsPointer). A runtime can remove information
151 /// that it stores in the spare bits of such pointers, or follow a reference
152 /// that refers to the instance indirectly.
153 virtual llvm::Expected<lldb::addr_t> FixupPointerValue(lldb::addr_t pointer,
154 CompilerType type) {
155 return pointer;
156 }
157
158 /// This allows a language runtime to adjust the location of \p variable
159 /// computed from its debug info, for storage whose indirection is only
160 /// known at runtime.
161 virtual llvm::Error FixupVariableLocation(Variable &variable, Value &value) {
162 return llvm::Error::success();
163 }
164
165 /// This allows a language runtime to evaluate
166 /// DW_OP_entry_value(\p entry_value) in \p frame without call site
167 /// information from the caller, when the runtime knows the value the
168 /// register had on entry. For example, a runtime that unwinds the frame
169 /// with GetRuntimeUnwindPlan() may know how that value relates to the CFA.
170 ///
171 /// \param[in,out] following
172 /// The operations after the DW_OP_entry_value. The runtime may
173 /// evaluate the first of them together with the entry value, and
174 /// consume them from the front, when it knows their combined result
175 /// but not the entry value alone.
176 ///
177 /// \return
178 /// The entry value, or std::nullopt to look it up in the call site
179 /// parameters of the caller.
180 virtual std::optional<Value>
181 EvaluateEntryValue(StackFrame &frame, llvm::ArrayRef<uint8_t> entry_value,
182 llvm::ArrayRef<uint8_t> &following) {
183 return std::nullopt;
184 }
185
186 virtual void SetExceptionBreakpoints() {}
187
189
190 virtual bool ExceptionBreakpointsAreSet() { return false; }
191
193 return false;
194 }
195
196 /// Create a breakpoint that catches an error escaping an expression
197 /// that runs as top-level code, such as a REPL line, where no
198 /// handler encloses it.
200
201 /// Return the error value that reached the breakpoint from
202 /// CreateErrorBackstopBreakpoint, with \p frame stopped there.
204 return {};
205 }
206
207 /// Given a frame, compute the location where its function would place an
208 /// error, if one is thrown by the function.
209 ///
210 /// \return
211 /// std::nullopt if the function cannot throw. A register location is
212 /// read in the frame the function returns to.
213 virtual std::optional<Value> GetThrownErrorLocation(StackFrame &frame) {
214 return std::nullopt;
215 }
216
217 /// Given \p frame, the frame a callee returned to, and \p location,
218 /// computed by GetThrownErrorLocation from the callee's frame, get the
219 /// error the callee threw.
220 ///
221 /// \return
222 /// std::nullopt if the callee did not throw. Otherwise the thrown
223 /// error, which is null if the error could not be read.
224 virtual std::optional<lldb::ValueObjectSP>
225 GetThrownError(StackFrame &frame, const Value &location) {
226 return std::nullopt;
227 }
228
229 /// A description of how to find an error thrown by a function. It is
230 /// computed from the callee's frame, and is only meaningful in the frame the
231 /// callee returns to.
233 public:
234 explicit ThrownErrorLocation(lldb::StackFrameSP callee_frame_sp = nullptr);
235
236 /// See LanguageRuntime::GetThrownError.
237 std::optional<lldb::ValueObjectSP>
238 GetThrownError(lldb::StackFrameSP caller_frame_sp) const;
239
240 private:
242 std::optional<Value> m_location;
243 };
244
245 static lldb::BreakpointSP
247 bool catch_bp, bool throw_bp,
248 bool is_internal = false);
249
251 GetExceptionPrecondition(lldb::LanguageType language, bool throw_bp);
252
257
262
263 virtual DeclVendor *GetDeclVendor() { return nullptr; }
264
267 bool catch_bp, bool throw_bp) = 0;
268
270 return m_process->GetTarget().GetSearchFilterForModule(nullptr);
271 }
272
273 virtual std::optional<uint64_t>
274 GetTypeBitSize(const CompilerType &compiler_type) {
275 return {};
276 }
277
278 virtual void SymbolsDidLoad(const ModuleList &module_list) {}
279
281 bool stop_others) = 0;
282
283 /// Identify whether a name is a runtime value that should not be hidden by
284 /// from the user interface.
285 virtual bool IsAllowedRuntimeValue(llvm::StringRef name) { return false; }
286
287 virtual std::optional<CompilerType> GetRuntimeType(CompilerType base_type) {
288 return std::nullopt;
289 }
290
291 void ModulesDidLoad(const ModuleList &module_list) override {}
292
293 // Called by ClangExpressionParser::PrepareForExecution to query for any
294 // custom LLVM IR passes that need to be run before an expression is
295 // assembled and run.
296 virtual bool GetIRPasses(LLVMUserExpression::IRPasses &custom_passes) {
297 return false;
298 }
299
300 virtual bool IsSymbolARuntimeThunk(const Symbol &symbol) { return false; }
301
302 // Given the name of a runtime symbol (e.g. in Objective-C, an ivar offset
303 // symbol), try to determine from the runtime what the value of that symbol
304 // would be. Useful when the underlying binary is stripped.
305 virtual lldb::addr_t LookupRuntimeSymbol(llvm::StringRef name) {
307 }
308
309 virtual bool isA(const void *ClassID) const { return ClassID == &ID; }
310 static char ID;
311
312 /// A language runtime may be able to provide a special UnwindPlan for
313 /// the frame represented by the register contents \a regctx when that
314 /// frame is not following the normal ABI conventions.
315 /// Instead of using the normal UnwindPlan for the function, we will use
316 /// this special UnwindPlan for this one backtrace.
317 /// One example of this would be a language that has asynchronous functions,
318 /// functions that may not be currently-executing, while waiting on other
319 /// asynchronous calls they made, but are part of a logical backtrace that
320 /// we want to show the developer because that's how they think of the
321 /// program flow.
322 ///
323 /// \param[in] thread
324 /// The thread that the unwind is happening on.
325 ///
326 /// \param[in] regctx
327 /// The RegisterContext for the frame we need to create an UnwindPlan.
328 /// We don't yet have a StackFrame when we're selecting the UnwindPlan.
329 ///
330 /// \param[out] behaves_like_zeroth_frame
331 /// With normal ABI calls, all stack frames except the zeroth frame need
332 /// to have the return-pc value backed up by 1 for symbolication purposes.
333 /// For these LanguageRuntime unwind plans, they may not follow normal ABI
334 /// calling conventions and the return pc may need to be symbolicated
335 /// as-is.
336 ///
337 /// \return
338 /// Returns an UnwindPlan to find the caller frame if it should be used,
339 /// instead of the UnwindPlan that would normally be used for this
340 /// function.
341 static lldb::UnwindPlanSP
344 bool &behaves_like_zeroth_frame);
345
346 /// Compares the frames identified by \p lhs and \p rhs, if a language
347 /// runtime of \p process can order them. For example, a runtime whose unwind
348 /// plans produce call frame addresses that aren't stack addresses knows how
349 /// those frames relate.
350 ///
351 /// \return
352 /// Younger if \p lhs is younger than \p rhs, Older if it is older, and
353 /// NoOpinion if no language runtime orders the frames.
354 static CFAComparison CompareCFAs(Process &process, const StackID &lhs,
355 const StackID &rhs);
356
357 /// Language runtime plugins can use this API to report
358 /// language-specific runtime information about this compile unit,
359 /// such as additional language version details or feature flags.
361
362protected:
363 // The static GetRuntimeUnwindPlan method above is only implemented in the
364 // base class; subclasses may override this protected member if they can
365 // provide one of these UnwindPlans.
366 virtual lldb::UnwindPlanSP
369 bool &behaves_like_zeroth_frame) {
370 return lldb::UnwindPlanSP();
371 }
372
373 // The static CompareCFAs method above is only implemented in the base
374 // class; subclasses may override this protected member if they can order
375 // frames that StackID::IsYoungerThan() can't.
376 virtual CFAComparison CompareCFAs(const StackID &lhs, const StackID &rhs) {
378 }
379
380 LanguageRuntime(Process *process);
381};
382
383} // namespace lldb_private
384
385#endif // LLDB_TARGET_LANGUAGERUNTIME_H
static llvm::raw_ostream & error(Stream &strm)
A section + offset based address class.
Definition Address.h:62
Generic representation of a type in a programming language.
static SearchFilter * CreateFromStructuredData(Target &target, const StructuredData::Dictionary &data_dict, Status &error)
bool ModulePasses(const lldb::ModuleSP &module_sp) override
Call this method with a Module to see if that module passes the filter.
ExceptionSearchFilter(const lldb::TargetSP &target_sp, lldb::LanguageType language, bool update_module_list=true)
void Search(Searcher &searcher) override
Call this method to do the search using the Searcher.
~ExceptionSearchFilter() override=default
std::unique_ptr< SearchFilter > DoCreateCopy() override
StructuredData::ObjectSP SerializeToStructuredData() override
void GetDescription(Stream *s) override
Prints a canonical description for the search filter to the stream s.
"lldb/Target/ExecutionContextScope.h" Inherit from this if your object can reconstruct its execution ...
A file utility class.
Definition FileSpec.h:56
std::optional< lldb::ValueObjectSP > GetThrownError(lldb::StackFrameSP caller_frame_sp) const
See LanguageRuntime::GetThrownError.
ThrownErrorLocation(lldb::StackFrameSP callee_frame_sp=nullptr)
virtual llvm::Error FixupVariableLocation(Variable &variable, Value &value)
This allows a language runtime to adjust the location of variable computed from its debug info,...
virtual StructuredData::ObjectSP GetLanguageSpecificData(SymbolContext sc)
Language runtime plugins can use this API to report language-specific runtime information about this ...
virtual bool IsSymbolARuntimeThunk(const Symbol &symbol)
virtual std::optional< CompilerType > GetRuntimeType(CompilerType base_type)
virtual lldb::LanguageType GetLanguageType() const =0
static CFAComparison CompareCFAs(Process &process, const StackID &lhs, const StackID &rhs)
Compares the frames identified by lhs and rhs, if a language runtime of process can order them.
virtual TypeAndOrName FixUpDynamicType(const TypeAndOrName &type_and_or_name, ValueObject &static_value)=0
virtual lldb::ThreadSP GetBacktraceThreadFromException(lldb::ValueObjectSP thread_sp)
virtual bool GetDynamicTypeAndAddress(ValueObject &in_value, lldb::DynamicValueType use_dynamic, TypeAndOrName &class_type_or_name, Address &address, Value::ValueType &value_type, llvm::ArrayRef< uint8_t > &local_buffer)=0
This call should return true if it could set the name and/or the type Sets address to the address of ...
virtual lldb::UnwindPlanSP GetRuntimeUnwindPlan(lldb::ProcessSP process_sp, lldb_private::RegisterContext *regctx, bool &behaves_like_zeroth_frame)
virtual lldb::ValueObjectSP GetExceptionObjectForThread(lldb::ThreadSP thread_sp)
static LanguageRuntime * FindPlugin(Process *process, lldb::LanguageType language)
virtual lldb::ValueObjectSP GetErrorValueAtBackstop(StackFrame &frame)
Return the error value that reached the breakpoint from CreateErrorBackstopBreakpoint,...
virtual CFAComparison CompareCFAs(const StackID &lhs, const StackID &rhs)
virtual bool ExceptionBreakpointsExplainStop(lldb::StopInfoSP stop_reason)
virtual bool IsAllowedRuntimeValue(llvm::StringRef name)
Identify whether a name is a runtime value that should not be hidden by from the user interface.
virtual LanguageRuntime * GetPreferredLanguageRuntime(ValueObject &in_value)
Return the preferred language runtime instance, which in most cases will be the current instance.
virtual lldb::ThreadPlanSP GetStepThroughTrampolinePlan(Thread &thread, bool stop_others)=0
virtual bool isA(const void *ClassID) const
virtual std::optional< Value > GetThrownErrorLocation(StackFrame &frame)
Given a frame, compute the location where its function would place an error, if one is thrown by the ...
virtual bool ExceptionBreakpointsAreSet()
static lldb::UnwindPlanSP GetRuntimeUnwindPlan(lldb_private::Thread &thread, lldb_private::RegisterContext *regctx, bool &behaves_like_zeroth_frame)
A language runtime may be able to provide a special UnwindPlan for the frame represented by the regis...
virtual llvm::Expected< VTableInfo > GetVTableInfo(ValueObject &in_value, bool check_type)
Get the vtable information for a given value.
virtual llvm::Expected< lldb::addr_t > FixupPointerValue(lldb::addr_t pointer, CompilerType type)
Returns the address of the instance that pointer refers to, where pointer is the value of an object o...
void ModulesDidLoad(const ModuleList &module_list) override
Called when modules have been loaded in the process.
virtual DeclVendor * GetDeclVendor()
virtual lldb::addr_t LookupRuntimeSymbol(llvm::StringRef name)
virtual void SymbolsDidLoad(const ModuleList &module_list)
virtual std::optional< uint64_t > GetTypeBitSize(const CompilerType &compiler_type)
virtual lldb::BreakpointSP CreateErrorBackstopBreakpoint()
Create a breakpoint that catches an error escaping an expression that runs as top-level code,...
virtual bool IsPreferredLanguageRuntimeFor(ValueObject &in_value)
Returns whether this runtime is the preferred runtime for in_value, an object of another language,...
static lldb::BreakpointSP CreateExceptionBreakpoint(Target &target, lldb::LanguageType language, bool catch_bp, bool throw_bp, bool is_internal=false)
virtual bool GetIRPasses(LLVMUserExpression::IRPasses &custom_passes)
static void InitializeCommands(CommandObject *parent)
virtual bool CouldHaveDynamicValue(ValueObject &in_value)=0
virtual llvm::Error GetObjectDescription(Stream &str, Value &value, ExecutionContextScope *exe_scope)=0
virtual llvm::Error GetObjectDescription(Stream &str, ValueObject &object)=0
virtual std::optional< Value > EvaluateEntryValue(StackFrame &frame, llvm::ArrayRef< uint8_t > entry_value, llvm::ArrayRef< uint8_t > &following)
This allows a language runtime to evaluate DW_OP_entry_value(entry_value) in frame without call site ...
virtual lldb::SearchFilterSP CreateExceptionSearchFilter()
virtual CompilerType GetConcreteType(ExecutionContextScope *exe_scope, llvm::StringRef abstract_type_name)
virtual lldb::BreakpointResolverSP CreateExceptionResolver(const lldb::BreakpointSP &bkpt, bool catch_bp, bool throw_bp)=0
virtual std::optional< lldb::ValueObjectSP > GetThrownError(StackFrame &frame, const Value &location)
Given frame, the frame a callee returned to, and location, computed by GetThrownErrorLocation from th...
static lldb::BreakpointPreconditionSP GetExceptionPrecondition(lldb::LanguageType language, bool throw_bp)
A collection class for Module objects.
Definition ModuleList.h:139
A plug-in interface definition class for debugging a process.
Definition Process.h:370
Process * m_process
Definition Runtime.h:29
Runtime(Process *process)
Definition Runtime.h:17
SearchFilter(const lldb::TargetSP &target_sp)
The basic constructor takes a Target, which gives the space to search.
General Outline: Provides the callback and search depth for the SearchFilter search.
This base class provides an interface to stack frames.
Definition StackFrame.h:44
An error handling class.
Definition Status.h:118
A stream class that can stream formatted output to a file.
Definition Stream.h:28
std::shared_ptr< Object > ObjectSP
Defines a symbol context baton that can be handed other debug core functions.
Sometimes you can find the name of the type corresponding to an object, but we don't have debug infor...
ValueType
Type that describes Value::m_value.
Definition Value.h:42
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
CFAComparison
How the call frame addresses of two frames order them.
Definition StackID.h:19
std::shared_ptr< lldb_private::ThreadPlan > ThreadPlanSP
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
std::shared_ptr< lldb_private::SearchFilter > SearchFilterSP
std::shared_ptr< lldb_private::BreakpointResolver > BreakpointResolverSP
std::shared_ptr< lldb_private::Thread > ThreadSP
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
LanguageType
Programming language type.
std::shared_ptr< lldb_private::Breakpoint > BreakpointSP
std::shared_ptr< lldb_private::Process > ProcessSP
std::shared_ptr< lldb_private::BreakpointPrecondition > BreakpointPreconditionSP
std::shared_ptr< lldb_private::UnwindPlan > UnwindPlanSP
std::shared_ptr< lldb_private::StopInfo > StopInfoSP
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::Target > TargetSP
std::shared_ptr< lldb_private::Module > ModuleSP
Symbol * symbol
Address of the vtable's virtual function table.