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 liblldb_LanguageRuntime_h_
11#define liblldb_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/lldb-private.h"
22#include "lldb/lldb-public.h"
23
24#include "clang/Basic/TargetOptions.h"
25
26namespace lldb_private {
27
28class ExceptionSearchFilter : public SearchFilter {
29public:
30  ExceptionSearchFilter(const lldb::TargetSP &target_sp,
31                        lldb::LanguageType language,
32                        bool update_module_list = true);
33
34  ~ExceptionSearchFilter() override = default;
35
36  bool ModulePasses(const lldb::ModuleSP &module_sp) override;
37
38  bool ModulePasses(const FileSpec &spec) override;
39
40  void Search(Searcher &searcher) override;
41
42  void GetDescription(Stream *s) override;
43
44  static SearchFilter *
45  CreateFromStructuredData(Target &target,
46                           const StructuredData::Dictionary &data_dict,
47                           Status &error);
48
49  StructuredData::ObjectSP SerializeToStructuredData() override;
50
51protected:
52  lldb::LanguageType m_language;
53  LanguageRuntime *m_language_runtime;
54  lldb::SearchFilterSP m_filter_sp;
55
56  lldb::SearchFilterSP DoCopyForBreakpoint(Breakpoint &breakpoint) override;
57
58  void UpdateModuleListIfNeeded();
59};
60
61class LanguageRuntime : public PluginInterface {
62public:
63  ~LanguageRuntime() override;
64
65  static LanguageRuntime *FindPlugin(Process *process,
66                                     lldb::LanguageType language);
67
68  static void InitializeCommands(CommandObject *parent);
69
70  virtual lldb::LanguageType GetLanguageType() const = 0;
71
72  virtual bool GetObjectDescription(Stream &str, ValueObject &object) = 0;
73
74  virtual bool GetObjectDescription(Stream &str, Value &value,
75                                    ExecutionContextScope *exe_scope) = 0;
76
77  // this call should return true if it could set the name and/or the type
78  virtual bool GetDynamicTypeAndAddress(ValueObject &in_value,
79                                        lldb::DynamicValueType use_dynamic,
80                                        TypeAndOrName &class_type_or_name,
81                                        Address &address,
82                                        Value::ValueType &value_type) = 0;
83
84  // This call should return a CompilerType given a generic type name and an
85  // ExecutionContextScope in which one can actually fetch any specialization
86  // information required.
87  virtual CompilerType GetConcreteType(ExecutionContextScope *exe_scope,
88                                       ConstString abstract_type_name) {
89    return CompilerType();
90  }
91
92  // This should be a fast test to determine whether it is likely that this
93  // value would have a dynamic type.
94  virtual bool CouldHaveDynamicValue(ValueObject &in_value) = 0;
95
96  // The contract for GetDynamicTypeAndAddress() is to return a "bare-bones"
97  // dynamic type For instance, given a Base* pointer,
98  // GetDynamicTypeAndAddress() will return the type of Derived, not Derived*.
99  // The job of this API is to correct this misalignment between the static
100  // type and the discovered dynamic type
101  virtual TypeAndOrName FixUpDynamicType(const TypeAndOrName &type_and_or_name,
102                                         ValueObject &static_value) = 0;
103
104  virtual void SetExceptionBreakpoints() {}
105
106  virtual void ClearExceptionBreakpoints() {}
107
108  virtual bool ExceptionBreakpointsAreSet() { return false; }
109
110  virtual bool ExceptionBreakpointsExplainStop(lldb::StopInfoSP stop_reason) {
111    return false;
112  }
113
114  static lldb::BreakpointSP
115  CreateExceptionBreakpoint(Target &target, lldb::LanguageType language,
116                            bool catch_bp, bool throw_bp,
117                            bool is_internal = false);
118
119  static lldb::BreakpointPreconditionSP
120  GetExceptionPrecondition(lldb::LanguageType language, bool throw_bp);
121
122  virtual lldb::ValueObjectSP GetExceptionObjectForThread(
123      lldb::ThreadSP thread_sp) {
124    return lldb::ValueObjectSP();
125  }
126
127  virtual lldb::ThreadSP GetBacktraceThreadFromException(
128      lldb::ValueObjectSP thread_sp) {
129    return lldb::ThreadSP();
130  }
131
132  Process *GetProcess() { return m_process; }
133
134  Target &GetTargetRef() { return m_process->GetTarget(); }
135
136  virtual DeclVendor *GetDeclVendor() { return nullptr; }
137
138  virtual lldb::BreakpointResolverSP
139  CreateExceptionResolver(Breakpoint *bkpt, bool catch_bp, bool throw_bp) = 0;
140
141  virtual lldb::SearchFilterSP CreateExceptionSearchFilter() {
142    return m_process->GetTarget().GetSearchFilterForModule(nullptr);
143  }
144
145  virtual bool GetTypeBitSize(const CompilerType &compiler_type,
146                              uint64_t &size) {
147    return false;
148  }
149
150  virtual void SymbolsDidLoad(const ModuleList &module_list) { return; }
151
152  virtual lldb::ThreadPlanSP GetStepThroughTrampolinePlan(Thread &thread,
153                                                          bool stop_others) = 0;
154
155  /// Identify whether a name is a runtime value that should not be hidden by
156  /// from the user interface.
157  virtual bool IsWhitelistedRuntimeValue(ConstString name) { return false; }
158
159  virtual llvm::Optional<CompilerType> GetRuntimeType(CompilerType base_type) {
160    return llvm::None;
161  }
162
163  virtual void ModulesDidLoad(const ModuleList &module_list) {}
164
165  // Called by the Clang expression evaluation engine to allow runtimes to
166  // alter the set of target options provided to the compiler. If the options
167  // prototype is modified, runtimes must return true, false otherwise.
168  virtual bool GetOverrideExprOptions(clang::TargetOptions &prototype) {
169    return false;
170  }
171
172  // Called by ClangExpressionParser::PrepareForExecution to query for any
173  // custom LLVM IR passes that need to be run before an expression is
174  // assembled and run.
175  virtual bool GetIRPasses(LLVMUserExpression::IRPasses &custom_passes) {
176    return false;
177  }
178
179  // Given the name of a runtime symbol (e.g. in Objective-C, an ivar offset
180  // symbol), try to determine from the runtime what the value of that symbol
181  // would be. Useful when the underlying binary is stripped.
182  virtual lldb::addr_t LookupRuntimeSymbol(ConstString name) {
183    return LLDB_INVALID_ADDRESS;
184  }
185
186  virtual bool isA(const void *ClassID) const { return ClassID == &ID; }
187  static char ID;
188
189protected:
190  // Classes that inherit from LanguageRuntime can see and modify these
191
192  LanguageRuntime(Process *process);
193  Process *m_process;
194
195private:
196  DISALLOW_COPY_AND_ASSIGN(LanguageRuntime);
197};
198
199} // namespace lldb_private
200
201#endif // liblldb_LanguageRuntime_h_
202