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
13#include "lldb/Breakpoint/BreakpointResolver.h"
14#include "lldb/Breakpoint/BreakpointResolverName.h"
15#include "lldb/Core/PluginInterface.h"
16#include "lldb/Core/Value.h"
17#include "lldb/Core/ValueObject.h"
18#include "lldb/Expression/LLVMUserExpression.h"
19#include "lldb/Symbol/DeclVendor.h"
20#include "lldb/Target/ExecutionContextScope.h"
21#include "lldb/Target/Runtime.h"
22#include "lldb/lldb-private.h"
23#include "lldb/lldb-public.h"
24
25namespace lldb_private {
26
27class ExceptionSearchFilter : public SearchFilter {
28public:
29 ExceptionSearchFilter(const lldb::TargetSP &target_sp,
30 lldb::LanguageType language,
31 bool update_module_list = true);
32
33 ~ExceptionSearchFilter() override = default;
34
35 bool ModulePasses(const lldb::ModuleSP &module_sp) override;
36
37 bool ModulePasses(const FileSpec &spec) override;
38
39 void Search(Searcher &searcher) override;
40
41 void GetDescription(Stream *s) override;
42
43 static SearchFilter *
44 CreateFromStructuredData(Target &target,
45 const StructuredData::Dictionary &data_dict,
46 Status &error);
47
48 StructuredData::ObjectSP SerializeToStructuredData() override;
49
50protected:
51 lldb::LanguageType m_language;
52 LanguageRuntime *m_language_runtime;
53 lldb::SearchFilterSP m_filter_sp;
54
55 lldb::SearchFilterSP DoCreateCopy() override;
56
57 void UpdateModuleListIfNeeded();
58};
59
60class LanguageRuntime : public Runtime, public PluginInterface {
61public:
62 static LanguageRuntime *FindPlugin(Process *process,
63 lldb::LanguageType language);
64
65 static void InitializeCommands(CommandObject *parent);
66
67 virtual lldb::LanguageType GetLanguageType() const = 0;
68
69 virtual bool GetObjectDescription(Stream &str, ValueObject &object) = 0;
70
71 virtual bool GetObjectDescription(Stream &str, Value &value,
72 ExecutionContextScope *exe_scope) = 0;
73
74 // this call should return true if it could set the name and/or the type
75 virtual bool GetDynamicTypeAndAddress(ValueObject &in_value,
76 lldb::DynamicValueType use_dynamic,
77 TypeAndOrName &class_type_or_name,
78 Address &address,
79 Value::ValueType &value_type) = 0;
80
81 // This call should return a CompilerType given a generic type name and an
82 // ExecutionContextScope in which one can actually fetch any specialization
83 // information required.
84 virtual CompilerType GetConcreteType(ExecutionContextScope *exe_scope,
85 ConstString abstract_type_name) {
86 return CompilerType();
87 }
88
89 // This should be a fast test to determine whether it is likely that this
90 // value would have a dynamic type.
91 virtual bool CouldHaveDynamicValue(ValueObject &in_value) = 0;
92
93 // The contract for GetDynamicTypeAndAddress() is to return a "bare-bones"
94 // dynamic type For instance, given a Base* pointer,
95 // GetDynamicTypeAndAddress() will return the type of Derived, not Derived*.
96 // The job of this API is to correct this misalignment between the static
97 // type and the discovered dynamic type
98 virtual TypeAndOrName FixUpDynamicType(const TypeAndOrName &type_and_or_name,
99 ValueObject &static_value) = 0;
100
101 virtual void SetExceptionBreakpoints() {}
102
103 virtual void ClearExceptionBreakpoints() {}
104
105 virtual bool ExceptionBreakpointsAreSet() { return false; }
106
107 virtual bool ExceptionBreakpointsExplainStop(lldb::StopInfoSP stop_reason) {
108 return false;
109 }
110
111 static lldb::BreakpointSP
112 CreateExceptionBreakpoint(Target &target, lldb::LanguageType language,
113 bool catch_bp, bool throw_bp,
114 bool is_internal = false);
115
116 static lldb::BreakpointPreconditionSP
117 GetExceptionPrecondition(lldb::LanguageType language, bool throw_bp);
118
119 virtual lldb::ValueObjectSP GetExceptionObjectForThread(
120 lldb::ThreadSP thread_sp) {
121 return lldb::ValueObjectSP();
122 }
123
124 virtual lldb::ThreadSP GetBacktraceThreadFromException(
125 lldb::ValueObjectSP thread_sp) {
126 return lldb::ThreadSP();
127 }
128
129 virtual DeclVendor *GetDeclVendor() { return nullptr; }
130
131 virtual lldb::BreakpointResolverSP
132 CreateExceptionResolver(const lldb::BreakpointSP &bkpt,
133 bool catch_bp, bool throw_bp) = 0;
134
135 virtual lldb::SearchFilterSP CreateExceptionSearchFilter() {
136 return m_process->GetTarget().GetSearchFilterForModule(nullptr);
137 }
138
139 virtual bool GetTypeBitSize(const CompilerType &compiler_type,
140 uint64_t &size) {
141 return false;
142 }
143
144 virtual void SymbolsDidLoad(const ModuleList &module_list) { return; }
145
146 virtual lldb::ThreadPlanSP GetStepThroughTrampolinePlan(Thread &thread,
147 bool stop_others) = 0;
148
149 /// Identify whether a name is a runtime value that should not be hidden by
150 /// from the user interface.
151 virtual bool IsAllowedRuntimeValue(ConstString name) { return false; }
152
153 virtual llvm::Optional<CompilerType> GetRuntimeType(CompilerType base_type) {
154 return llvm::None;
155 }
156
157 virtual void ModulesDidLoad(const ModuleList &module_list) override {}
158
159 // Called by ClangExpressionParser::PrepareForExecution to query for any
160 // custom LLVM IR passes that need to be run before an expression is
161 // assembled and run.
162 virtual bool GetIRPasses(LLVMUserExpression::IRPasses &custom_passes) {
163 return false;
164 }
165
166 // Given the name of a runtime symbol (e.g. in Objective-C, an ivar offset
167 // symbol), try to determine from the runtime what the value of that symbol
168 // would be. Useful when the underlying binary is stripped.
169 virtual lldb::addr_t LookupRuntimeSymbol(ConstString name) {
170 return LLDB_INVALID_ADDRESS;
171 }
172
173 virtual bool isA(const void *ClassID) const { return ClassID == &ID; }
174 static char ID;
175
176 /// A language runtime may be able to provide a special UnwindPlan for
177 /// the frame represented by the register contents \a regctx when that
178 /// frame is not following the normal ABI conventions.
179 /// Instead of using the normal UnwindPlan for the function, we will use
180 /// this special UnwindPlan for this one backtrace.
181 /// One example of this would be a language that has asynchronous functions,
182 /// functions that may not be currently-executing, while waiting on other
183 /// asynchronous calls they made, but are part of a logical backtrace that
184 /// we want to show the developer because that's how they think of the
185 /// program flow.
186 ///
187 /// \param[in] thread
188 /// The thread that the unwind is happening on.
189 ///
190 /// \param[in] regctx
191 /// The RegisterContext for the frame we need to create an UnwindPlan.
192 /// We don't yet have a StackFrame when we're selecting the UnwindPlan.
193 ///
194 /// \param[out] behaves_like_zeroth_frame
195 /// With normal ABI calls, all stack frames except the zeroth frame need
196 /// to have the return-pc value backed up by 1 for symbolication purposes.
197 /// For these LanguageRuntime unwind plans, they may not follow normal ABI
198 /// calling conventions and the return pc may need to be symbolicated
199 /// as-is.
200 ///
201 /// \return
202 /// Returns an UnwindPlan to find the caller frame if it should be used,
203 /// instead of the UnwindPlan that would normally be used for this
204 /// function.
205 static lldb::UnwindPlanSP
206 GetRuntimeUnwindPlan(lldb_private::Thread &thread,
207 lldb_private::RegisterContext *regctx,
208 bool &behaves_like_zeroth_frame);
209
210protected:
211 // The static GetRuntimeUnwindPlan method above is only implemented in the
212 // base class; subclasses may override this protected member if they can
213 // provide one of these UnwindPlans.
214 virtual lldb::UnwindPlanSP
215 GetRuntimeUnwindPlan(lldb::ProcessSP process_sp,
216 lldb_private::RegisterContext *regctx,
217 bool &behaves_like_zeroth_frame) {
218 return lldb::UnwindPlanSP();
219 }
220
221 LanguageRuntime(Process *process);
222};
223
224} // namespace lldb_private
225
226#endif // LLDB_TARGET_LANGUAGERUNTIME_H
227