blob: f7d445997fade1edb7fd5ca4a2711c6696371fd1 [file] [log] [blame]
Raphael Isemann80814282020-01-24 08:23:27 +01001//===-- LanguageRuntime.cpp -----------------------------------------------===//
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/Target.h"
Jim Ingham22777012010-09-23 02:01:19 +000015
16using namespace lldb;
17using namespace lldb_private;
18
Alex Langford056f6f12019-06-08 18:45:00 +000019char LanguageRuntime::ID = 0;
20
Jim Inghame14dc262016-09-12 23:10:56 +000021ExceptionSearchFilter::ExceptionSearchFilter(const lldb::TargetSP &target_sp,
22 lldb::LanguageType language,
23 bool update_module_list)
24 : SearchFilter(target_sp, FilterTy::Exception), m_language(language),
25 m_language_runtime(nullptr), m_filter_sp() {
26 if (update_module_list)
Kate Stoneb9c1b512016-09-06 20:57:50 +000027 UpdateModuleListIfNeeded();
Jim Inghame14dc262016-09-12 23:10:56 +000028}
Greg Claytonbff78252013-03-11 18:42:51 +000029
Jim Inghame14dc262016-09-12 23:10:56 +000030bool ExceptionSearchFilter::ModulePasses(const lldb::ModuleSP &module_sp) {
31 UpdateModuleListIfNeeded();
32 if (m_filter_sp)
33 return m_filter_sp->ModulePasses(module_sp);
34 return false;
35}
Kate Stoneb9c1b512016-09-06 20:57:50 +000036
Jim Inghame14dc262016-09-12 23:10:56 +000037bool ExceptionSearchFilter::ModulePasses(const FileSpec &spec) {
38 UpdateModuleListIfNeeded();
39 if (m_filter_sp)
40 return m_filter_sp->ModulePasses(spec);
41 return false;
42}
Kate Stoneb9c1b512016-09-06 20:57:50 +000043
Jim Inghame14dc262016-09-12 23:10:56 +000044void ExceptionSearchFilter::Search(Searcher &searcher) {
45 UpdateModuleListIfNeeded();
46 if (m_filter_sp)
47 m_filter_sp->Search(searcher);
48}
Kate Stoneb9c1b512016-09-06 20:57:50 +000049
Jim Inghame14dc262016-09-12 23:10:56 +000050void ExceptionSearchFilter::GetDescription(Stream *s) {
51 UpdateModuleListIfNeeded();
52 if (m_filter_sp)
53 m_filter_sp->GetDescription(s);
54}
Greg Claytonbff78252013-03-11 18:42:51 +000055
Jim Inghame14dc262016-09-12 23:10:56 +000056void ExceptionSearchFilter::UpdateModuleListIfNeeded() {
57 ProcessSP process_sp(m_target_sp->GetProcessSP());
58 if (process_sp) {
59 bool refreash_filter = !m_filter_sp;
60 if (m_language_runtime == nullptr) {
61 m_language_runtime = process_sp->GetLanguageRuntime(m_language);
62 refreash_filter = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +000063 } else {
Jim Inghame14dc262016-09-12 23:10:56 +000064 LanguageRuntime *language_runtime =
65 process_sp->GetLanguageRuntime(m_language);
66 if (m_language_runtime != language_runtime) {
67 m_language_runtime = language_runtime;
68 refreash_filter = true;
69 }
Greg Claytonbff78252013-03-11 18:42:51 +000070 }
Jim Inghame14dc262016-09-12 23:10:56 +000071
72 if (refreash_filter && m_language_runtime) {
73 m_filter_sp = m_language_runtime->CreateExceptionSearchFilter();
74 }
75 } else {
76 m_filter_sp.reset();
77 m_language_runtime = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +000078 }
Jim Inghame14dc262016-09-12 23:10:56 +000079}
80
Tatyana Krasnukha7fb06792020-02-12 16:16:57 +030081SearchFilterSP ExceptionSearchFilter::DoCreateCopy() {
Jim Inghame14dc262016-09-12 23:10:56 +000082 return SearchFilterSP(
83 new ExceptionSearchFilter(TargetSP(), m_language, false));
84}
85
86SearchFilter *ExceptionSearchFilter::CreateFromStructuredData(
Zachary Turner97206d52017-05-12 04:51:55 +000087 Target &target, const StructuredData::Dictionary &data_dict,
88 Status &error) {
Jim Inghame14dc262016-09-12 23:10:56 +000089 SearchFilter *result = nullptr;
90 return result;
91}
92
93StructuredData::ObjectSP ExceptionSearchFilter::SerializeToStructuredData() {
94 StructuredData::ObjectSP result_sp;
95
96 return result_sp;
97}
Greg Claytonbff78252013-03-11 18:42:51 +000098
99// The Target is the one that knows how to create breakpoints, so this function
Kate Stoneb9c1b512016-09-06 20:57:50 +0000100// is meant to be used either by the target or internally in
101// Set/ClearExceptionBreakpoints.
102class ExceptionBreakpointResolver : public BreakpointResolver {
Greg Claytonbff78252013-03-11 18:42:51 +0000103public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104 ExceptionBreakpointResolver(lldb::LanguageType language, bool catch_bp,
105 bool throw_bp)
106 : BreakpointResolver(nullptr, BreakpointResolver::ExceptionResolver),
107 m_language(language), m_language_runtime(nullptr), m_catch_bp(catch_bp),
108 m_throw_bp(throw_bp) {}
Greg Claytonbff78252013-03-11 18:42:51 +0000109
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110 ~ExceptionBreakpointResolver() override = default;
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000111
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112 Searcher::CallbackReturn SearchCallback(SearchFilter &filter,
Raphael Isemann95e264f2019-10-10 11:26:51 +0000113 SymbolContext &context,
114 Address *addr) override {
Greg Claytonbff78252013-03-11 18:42:51 +0000115
Kate Stoneb9c1b512016-09-06 20:57:50 +0000116 if (SetActualResolver())
Raphael Isemann95e264f2019-10-10 11:26:51 +0000117 return m_actual_resolver_sp->SearchCallback(filter, context, addr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118 else
119 return eCallbackReturnStop;
120 }
121
Jim Ingham4911d362018-09-07 18:43:04 +0000122 lldb::SearchDepth GetDepth() override {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123 if (SetActualResolver())
124 return m_actual_resolver_sp->GetDepth();
125 else
Jim Ingham4911d362018-09-07 18:43:04 +0000126 return lldb::eSearchDepthTarget;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127 }
128
129 void GetDescription(Stream *s) override {
130 Language *language_plugin = Language::FindPlugin(m_language);
131 if (language_plugin)
132 language_plugin->GetExceptionResolverDescription(m_catch_bp, m_throw_bp,
133 *s);
134 else
135 Language::GetDefaultExceptionResolverDescription(m_catch_bp, m_throw_bp,
136 *s);
137
138 SetActualResolver();
139 if (m_actual_resolver_sp) {
140 s->Printf(" using: ");
141 m_actual_resolver_sp->GetDescription(s);
142 } else
143 s->Printf(" the correct runtime exception handler will be determined "
144 "when you run");
145 }
146
147 void Dump(Stream *s) const override {}
148
149 /// Methods for support type inquiry through isa, cast, and dyn_cast:
150 static inline bool classof(const BreakpointResolverName *) { return true; }
151 static inline bool classof(const BreakpointResolver *V) {
152 return V->getResolverID() == BreakpointResolver::ExceptionResolver;
153 }
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000154
Greg Claytonbff78252013-03-11 18:42:51 +0000155protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156 BreakpointResolverSP CopyForBreakpoint(Breakpoint &breakpoint) override {
Martin Svensson0b0dca92019-11-22 11:18:47 +0100157 BreakpointResolverSP ret_sp(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158 new ExceptionBreakpointResolver(m_language, m_catch_bp, m_throw_bp));
Martin Svensson0b0dca92019-11-22 11:18:47 +0100159 ret_sp->SetBreakpoint(&breakpoint);
160 return ret_sp;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161 }
Jim Ingham33df7cd2014-12-06 01:28:03 +0000162
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163 bool SetActualResolver() {
164 ProcessSP process_sp;
165 if (m_breakpoint) {
166 process_sp = m_breakpoint->GetTarget().GetProcessSP();
167 if (process_sp) {
168 bool refreash_resolver = !m_actual_resolver_sp;
169 if (m_language_runtime == nullptr) {
170 m_language_runtime = process_sp->GetLanguageRuntime(m_language);
171 refreash_resolver = true;
172 } else {
173 LanguageRuntime *language_runtime =
174 process_sp->GetLanguageRuntime(m_language);
175 if (m_language_runtime != language_runtime) {
176 m_language_runtime = language_runtime;
177 refreash_resolver = true;
178 }
Greg Claytonbff78252013-03-11 18:42:51 +0000179 }
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000180
Kate Stoneb9c1b512016-09-06 20:57:50 +0000181 if (refreash_resolver && m_language_runtime) {
182 m_actual_resolver_sp = m_language_runtime->CreateExceptionResolver(
183 m_breakpoint, m_catch_bp, m_throw_bp);
184 }
185 } else {
186 m_actual_resolver_sp.reset();
187 m_language_runtime = nullptr;
188 }
189 } else {
190 m_actual_resolver_sp.reset();
191 m_language_runtime = nullptr;
192 }
193 return (bool)m_actual_resolver_sp;
194 }
195
196 lldb::BreakpointResolverSP m_actual_resolver_sp;
197 lldb::LanguageType m_language;
198 LanguageRuntime *m_language_runtime;
199 bool m_catch_bp;
200 bool m_throw_bp;
Greg Claytonbff78252013-03-11 18:42:51 +0000201};
202
Kate Stoneb9c1b512016-09-06 20:57:50 +0000203LanguageRuntime *LanguageRuntime::FindPlugin(Process *process,
204 lldb::LanguageType language) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000205 std::unique_ptr<LanguageRuntime> language_runtime_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000206 LanguageRuntimeCreateInstance create_callback;
Jim Ingham22777012010-09-23 02:01:19 +0000207
Kate Stoneb9c1b512016-09-06 20:57:50 +0000208 for (uint32_t idx = 0;
209 (create_callback =
210 PluginManager::GetLanguageRuntimeCreateCallbackAtIndex(idx)) !=
211 nullptr;
212 ++idx) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000213 language_runtime_up.reset(create_callback(process, language));
Jim Ingham22777012010-09-23 02:01:19 +0000214
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000215 if (language_runtime_up)
216 return language_runtime_up.release();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000217 }
Jim Ingham22777012010-09-23 02:01:19 +0000218
Kate Stoneb9c1b512016-09-06 20:57:50 +0000219 return nullptr;
Jim Ingham22777012010-09-23 02:01:19 +0000220}
221
Kate Stoneb9c1b512016-09-06 20:57:50 +0000222LanguageRuntime::LanguageRuntime(Process *process) : m_process(process) {}
Jim Ingham22777012010-09-23 02:01:19 +0000223
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000224LanguageRuntime::~LanguageRuntime() = default;
Jim Ingham219ba192012-03-05 04:47:34 +0000225
Alex Langford7f9c9f22019-06-21 19:43:07 +0000226BreakpointPreconditionSP
227LanguageRuntime::GetExceptionPrecondition(LanguageType language,
228 bool throw_bp) {
229 LanguageRuntimeCreateInstance create_callback;
230 for (uint32_t idx = 0;
231 (create_callback =
232 PluginManager::GetLanguageRuntimeCreateCallbackAtIndex(idx)) !=
233 nullptr;
234 idx++) {
235 if (auto precondition_callback =
236 PluginManager::GetLanguageRuntimeGetExceptionPreconditionAtIndex(
237 idx)) {
238 if (BreakpointPreconditionSP precond =
239 precondition_callback(language, throw_bp))
240 return precond;
241 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000242 }
Alex Langford7f9c9f22019-06-21 19:43:07 +0000243 return BreakpointPreconditionSP();
Jim Inghama72b31c2015-04-22 19:42:18 +0000244}
245
Kate Stoneb9c1b512016-09-06 20:57:50 +0000246BreakpointSP LanguageRuntime::CreateExceptionBreakpoint(
247 Target &target, lldb::LanguageType language, bool catch_bp, bool throw_bp,
248 bool is_internal) {
249 BreakpointResolverSP resolver_sp(
250 new ExceptionBreakpointResolver(language, catch_bp, throw_bp));
251 SearchFilterSP filter_sp(
252 new ExceptionSearchFilter(target.shared_from_this(), language));
253 bool hardware = false;
254 bool resolve_indirect_functions = false;
255 BreakpointSP exc_breakpt_sp(
256 target.CreateBreakpoint(filter_sp, resolver_sp, is_internal, hardware,
257 resolve_indirect_functions));
258 if (exc_breakpt_sp) {
Alex Langford7f9c9f22019-06-21 19:43:07 +0000259 if (auto precond = GetExceptionPrecondition(language, throw_bp))
260 exc_breakpt_sp->SetPrecondition(precond);
Jim Inghama72b31c2015-04-22 19:42:18 +0000261
Kate Stoneb9c1b512016-09-06 20:57:50 +0000262 if (is_internal)
263 exc_breakpt_sp->SetBreakpointKind("exception");
264 }
265
266 return exc_breakpt_sp;
Jim Ingham219ba192012-03-05 04:47:34 +0000267}
268
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269void LanguageRuntime::InitializeCommands(CommandObject *parent) {
270 if (!parent)
271 return;
Colin Rileyc9c55a22015-05-04 18:39:38 +0000272
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273 if (!parent->IsMultiwordObject())
274 return;
Colin Rileyc9c55a22015-05-04 18:39:38 +0000275
Kate Stoneb9c1b512016-09-06 20:57:50 +0000276 LanguageRuntimeCreateInstance create_callback;
Colin Rileyc9c55a22015-05-04 18:39:38 +0000277
Kate Stoneb9c1b512016-09-06 20:57:50 +0000278 for (uint32_t idx = 0;
279 (create_callback =
280 PluginManager::GetLanguageRuntimeCreateCallbackAtIndex(idx)) !=
281 nullptr;
282 ++idx) {
283 if (LanguageRuntimeGetCommandObject command_callback =
284 PluginManager::GetLanguageRuntimeGetCommandObjectAtIndex(idx)) {
285 CommandObjectSP command =
286 command_callback(parent->GetCommandInterpreter());
287 if (command) {
288 // the CommandObject vended by a Language plugin cannot be created once
Zachary Turnera4496982016-10-05 21:14:38 +0000289 // and cached because we may create multiple debuggers and need one
290 // instance of the command each - the implementing function is meant to
291 // create a new instance of the command each time it is invoked.
292 parent->LoadSubCommand(command->GetCommandName().str().c_str(), command);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000293 }
Colin Rileyc9c55a22015-05-04 18:39:38 +0000294 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000295 }
Colin Rileyc9c55a22015-05-04 18:39:38 +0000296}