blob: a75f0085498bf9a60aaa2e18569fd62d02167721 [file] [log] [blame]
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001//===-- LanguageRuntime.cpp -------------------------------------*- C++ -*-===//
Jim Ingham22777012010-09-23 02:01:19 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Jim Ingham22777012010-09-23 02:01:19 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/Target/LanguageRuntime.h"
10#include "lldb/Core/PluginManager.h"
Jim Ingham33df7cd2014-12-06 01:28:03 +000011#include "lldb/Core/SearchFilter.h"
Colin Rileyc9c55a22015-05-04 18:39:38 +000012#include "lldb/Interpreter/CommandInterpreter.h"
Alex Langfordd2284122019-05-16 22:01:25 +000013#include "lldb/Target/Language.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000014#include "lldb/Target/ObjCLanguageRuntime.h"
15#include "lldb/Target/Target.h"
Jim Ingham22777012010-09-23 02:01:19 +000016
17using namespace lldb;
18using namespace lldb_private;
19
Alex Langford056f6f12019-06-08 18:45:00 +000020char LanguageRuntime::ID = 0;
21
Jim Inghame14dc262016-09-12 23:10:56 +000022ExceptionSearchFilter::ExceptionSearchFilter(const lldb::TargetSP &target_sp,
23 lldb::LanguageType language,
24 bool update_module_list)
25 : SearchFilter(target_sp, FilterTy::Exception), m_language(language),
26 m_language_runtime(nullptr), m_filter_sp() {
27 if (update_module_list)
Kate Stoneb9c1b512016-09-06 20:57:50 +000028 UpdateModuleListIfNeeded();
Jim Inghame14dc262016-09-12 23:10:56 +000029}
Greg Claytonbff78252013-03-11 18:42:51 +000030
Jim Inghame14dc262016-09-12 23:10:56 +000031bool ExceptionSearchFilter::ModulePasses(const lldb::ModuleSP &module_sp) {
32 UpdateModuleListIfNeeded();
33 if (m_filter_sp)
34 return m_filter_sp->ModulePasses(module_sp);
35 return false;
36}
Kate Stoneb9c1b512016-09-06 20:57:50 +000037
Jim Inghame14dc262016-09-12 23:10:56 +000038bool ExceptionSearchFilter::ModulePasses(const FileSpec &spec) {
39 UpdateModuleListIfNeeded();
40 if (m_filter_sp)
41 return m_filter_sp->ModulePasses(spec);
42 return false;
43}
Kate Stoneb9c1b512016-09-06 20:57:50 +000044
Jim Inghame14dc262016-09-12 23:10:56 +000045void ExceptionSearchFilter::Search(Searcher &searcher) {
46 UpdateModuleListIfNeeded();
47 if (m_filter_sp)
48 m_filter_sp->Search(searcher);
49}
Kate Stoneb9c1b512016-09-06 20:57:50 +000050
Jim Inghame14dc262016-09-12 23:10:56 +000051void ExceptionSearchFilter::GetDescription(Stream *s) {
52 UpdateModuleListIfNeeded();
53 if (m_filter_sp)
54 m_filter_sp->GetDescription(s);
55}
Greg Claytonbff78252013-03-11 18:42:51 +000056
Jim Inghame14dc262016-09-12 23:10:56 +000057void ExceptionSearchFilter::UpdateModuleListIfNeeded() {
58 ProcessSP process_sp(m_target_sp->GetProcessSP());
59 if (process_sp) {
60 bool refreash_filter = !m_filter_sp;
61 if (m_language_runtime == nullptr) {
62 m_language_runtime = process_sp->GetLanguageRuntime(m_language);
63 refreash_filter = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 } else {
Jim Inghame14dc262016-09-12 23:10:56 +000065 LanguageRuntime *language_runtime =
66 process_sp->GetLanguageRuntime(m_language);
67 if (m_language_runtime != language_runtime) {
68 m_language_runtime = language_runtime;
69 refreash_filter = true;
70 }
Greg Claytonbff78252013-03-11 18:42:51 +000071 }
Jim Inghame14dc262016-09-12 23:10:56 +000072
73 if (refreash_filter && m_language_runtime) {
74 m_filter_sp = m_language_runtime->CreateExceptionSearchFilter();
75 }
76 } else {
77 m_filter_sp.reset();
78 m_language_runtime = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +000079 }
Jim Inghame14dc262016-09-12 23:10:56 +000080}
81
82SearchFilterSP
83ExceptionSearchFilter::DoCopyForBreakpoint(Breakpoint &breakpoint) {
84 return SearchFilterSP(
85 new ExceptionSearchFilter(TargetSP(), m_language, false));
86}
87
88SearchFilter *ExceptionSearchFilter::CreateFromStructuredData(
Zachary Turner97206d52017-05-12 04:51:55 +000089 Target &target, const StructuredData::Dictionary &data_dict,
90 Status &error) {
Jim Inghame14dc262016-09-12 23:10:56 +000091 SearchFilter *result = nullptr;
92 return result;
93}
94
95StructuredData::ObjectSP ExceptionSearchFilter::SerializeToStructuredData() {
96 StructuredData::ObjectSP result_sp;
97
98 return result_sp;
99}
Greg Claytonbff78252013-03-11 18:42:51 +0000100
101// The Target is the one that knows how to create breakpoints, so this function
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102// is meant to be used either by the target or internally in
103// Set/ClearExceptionBreakpoints.
104class ExceptionBreakpointResolver : public BreakpointResolver {
Greg Claytonbff78252013-03-11 18:42:51 +0000105public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106 ExceptionBreakpointResolver(lldb::LanguageType language, bool catch_bp,
107 bool throw_bp)
108 : BreakpointResolver(nullptr, BreakpointResolver::ExceptionResolver),
109 m_language(language), m_language_runtime(nullptr), m_catch_bp(catch_bp),
110 m_throw_bp(throw_bp) {}
Greg Claytonbff78252013-03-11 18:42:51 +0000111
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112 ~ExceptionBreakpointResolver() override = default;
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000113
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114 Searcher::CallbackReturn SearchCallback(SearchFilter &filter,
115 SymbolContext &context, Address *addr,
116 bool containing) override {
Greg Claytonbff78252013-03-11 18:42:51 +0000117
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118 if (SetActualResolver())
119 return m_actual_resolver_sp->SearchCallback(filter, context, addr,
120 containing);
121 else
122 return eCallbackReturnStop;
123 }
124
Jim Ingham4911d362018-09-07 18:43:04 +0000125 lldb::SearchDepth GetDepth() override {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126 if (SetActualResolver())
127 return m_actual_resolver_sp->GetDepth();
128 else
Jim Ingham4911d362018-09-07 18:43:04 +0000129 return lldb::eSearchDepthTarget;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 }
131
132 void GetDescription(Stream *s) override {
133 Language *language_plugin = Language::FindPlugin(m_language);
134 if (language_plugin)
135 language_plugin->GetExceptionResolverDescription(m_catch_bp, m_throw_bp,
136 *s);
137 else
138 Language::GetDefaultExceptionResolverDescription(m_catch_bp, m_throw_bp,
139 *s);
140
141 SetActualResolver();
142 if (m_actual_resolver_sp) {
143 s->Printf(" using: ");
144 m_actual_resolver_sp->GetDescription(s);
145 } else
146 s->Printf(" the correct runtime exception handler will be determined "
147 "when you run");
148 }
149
150 void Dump(Stream *s) const override {}
151
152 /// Methods for support type inquiry through isa, cast, and dyn_cast:
153 static inline bool classof(const BreakpointResolverName *) { return true; }
154 static inline bool classof(const BreakpointResolver *V) {
155 return V->getResolverID() == BreakpointResolver::ExceptionResolver;
156 }
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000157
Greg Claytonbff78252013-03-11 18:42:51 +0000158protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000159 BreakpointResolverSP CopyForBreakpoint(Breakpoint &breakpoint) override {
160 return BreakpointResolverSP(
161 new ExceptionBreakpointResolver(m_language, m_catch_bp, m_throw_bp));
162 }
Jim Ingham33df7cd2014-12-06 01:28:03 +0000163
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164 bool SetActualResolver() {
165 ProcessSP process_sp;
166 if (m_breakpoint) {
167 process_sp = m_breakpoint->GetTarget().GetProcessSP();
168 if (process_sp) {
169 bool refreash_resolver = !m_actual_resolver_sp;
170 if (m_language_runtime == nullptr) {
171 m_language_runtime = process_sp->GetLanguageRuntime(m_language);
172 refreash_resolver = true;
173 } else {
174 LanguageRuntime *language_runtime =
175 process_sp->GetLanguageRuntime(m_language);
176 if (m_language_runtime != language_runtime) {
177 m_language_runtime = language_runtime;
178 refreash_resolver = true;
179 }
Greg Claytonbff78252013-03-11 18:42:51 +0000180 }
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000181
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182 if (refreash_resolver && m_language_runtime) {
183 m_actual_resolver_sp = m_language_runtime->CreateExceptionResolver(
184 m_breakpoint, m_catch_bp, m_throw_bp);
185 }
186 } else {
187 m_actual_resolver_sp.reset();
188 m_language_runtime = nullptr;
189 }
190 } else {
191 m_actual_resolver_sp.reset();
192 m_language_runtime = nullptr;
193 }
194 return (bool)m_actual_resolver_sp;
195 }
196
197 lldb::BreakpointResolverSP m_actual_resolver_sp;
198 lldb::LanguageType m_language;
199 LanguageRuntime *m_language_runtime;
200 bool m_catch_bp;
201 bool m_throw_bp;
Greg Claytonbff78252013-03-11 18:42:51 +0000202};
203
Kate Stoneb9c1b512016-09-06 20:57:50 +0000204LanguageRuntime *LanguageRuntime::FindPlugin(Process *process,
205 lldb::LanguageType language) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000206 std::unique_ptr<LanguageRuntime> language_runtime_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207 LanguageRuntimeCreateInstance create_callback;
Jim Ingham22777012010-09-23 02:01:19 +0000208
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209 for (uint32_t idx = 0;
210 (create_callback =
211 PluginManager::GetLanguageRuntimeCreateCallbackAtIndex(idx)) !=
212 nullptr;
213 ++idx) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000214 language_runtime_up.reset(create_callback(process, language));
Jim Ingham22777012010-09-23 02:01:19 +0000215
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000216 if (language_runtime_up)
217 return language_runtime_up.release();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000218 }
Jim Ingham22777012010-09-23 02:01:19 +0000219
Kate Stoneb9c1b512016-09-06 20:57:50 +0000220 return nullptr;
Jim Ingham22777012010-09-23 02:01:19 +0000221}
222
Kate Stoneb9c1b512016-09-06 20:57:50 +0000223LanguageRuntime::LanguageRuntime(Process *process) : m_process(process) {}
Jim Ingham22777012010-09-23 02:01:19 +0000224
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000225LanguageRuntime::~LanguageRuntime() = default;
Jim Ingham219ba192012-03-05 04:47:34 +0000226
Jim Inghama72b31c2015-04-22 19:42:18 +0000227Breakpoint::BreakpointPreconditionSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000228LanguageRuntime::CreateExceptionPrecondition(lldb::LanguageType language,
229 bool catch_bp, bool throw_bp) {
230 switch (language) {
231 case eLanguageTypeObjC:
232 if (throw_bp)
233 return Breakpoint::BreakpointPreconditionSP(
234 new ObjCLanguageRuntime::ObjCExceptionPrecondition());
235 break;
236 default:
237 break;
238 }
239 return Breakpoint::BreakpointPreconditionSP();
Jim Inghama72b31c2015-04-22 19:42:18 +0000240}
241
Kate Stoneb9c1b512016-09-06 20:57:50 +0000242BreakpointSP LanguageRuntime::CreateExceptionBreakpoint(
243 Target &target, lldb::LanguageType language, bool catch_bp, bool throw_bp,
244 bool is_internal) {
245 BreakpointResolverSP resolver_sp(
246 new ExceptionBreakpointResolver(language, catch_bp, throw_bp));
247 SearchFilterSP filter_sp(
248 new ExceptionSearchFilter(target.shared_from_this(), language));
249 bool hardware = false;
250 bool resolve_indirect_functions = false;
251 BreakpointSP exc_breakpt_sp(
252 target.CreateBreakpoint(filter_sp, resolver_sp, is_internal, hardware,
253 resolve_indirect_functions));
254 if (exc_breakpt_sp) {
255 Breakpoint::BreakpointPreconditionSP precondition_sp =
256 CreateExceptionPrecondition(language, catch_bp, throw_bp);
257 if (precondition_sp)
258 exc_breakpt_sp->SetPrecondition(precondition_sp);
Jim Inghama72b31c2015-04-22 19:42:18 +0000259
Kate Stoneb9c1b512016-09-06 20:57:50 +0000260 if (is_internal)
261 exc_breakpt_sp->SetBreakpointKind("exception");
262 }
263
264 return exc_breakpt_sp;
Jim Ingham219ba192012-03-05 04:47:34 +0000265}
266
Kate Stoneb9c1b512016-09-06 20:57:50 +0000267void LanguageRuntime::InitializeCommands(CommandObject *parent) {
268 if (!parent)
269 return;
Colin Rileyc9c55a22015-05-04 18:39:38 +0000270
Kate Stoneb9c1b512016-09-06 20:57:50 +0000271 if (!parent->IsMultiwordObject())
272 return;
Colin Rileyc9c55a22015-05-04 18:39:38 +0000273
Kate Stoneb9c1b512016-09-06 20:57:50 +0000274 LanguageRuntimeCreateInstance create_callback;
Colin Rileyc9c55a22015-05-04 18:39:38 +0000275
Kate Stoneb9c1b512016-09-06 20:57:50 +0000276 for (uint32_t idx = 0;
277 (create_callback =
278 PluginManager::GetLanguageRuntimeCreateCallbackAtIndex(idx)) !=
279 nullptr;
280 ++idx) {
281 if (LanguageRuntimeGetCommandObject command_callback =
282 PluginManager::GetLanguageRuntimeGetCommandObjectAtIndex(idx)) {
283 CommandObjectSP command =
284 command_callback(parent->GetCommandInterpreter());
285 if (command) {
286 // the CommandObject vended by a Language plugin cannot be created once
Zachary Turnera4496982016-10-05 21:14:38 +0000287 // and cached because we may create multiple debuggers and need one
288 // instance of the command each - the implementing function is meant to
289 // create a new instance of the command each time it is invoked.
290 parent->LoadSubCommand(command->GetCommandName().str().c_str(), command);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000291 }
Colin Rileyc9c55a22015-05-04 18:39:38 +0000292 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000293 }
Colin Rileyc9c55a22015-05-04 18:39:38 +0000294}
295