blob: f2e2febf8b88ddf9af39318eccda7cac03b6d18f [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:
Tatyana Krasnukha6c17cc52020-03-03 13:29:12 +0300156 BreakpointResolverSP CopyForBreakpoint(BreakpointSP &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));
Tatyana Krasnukha6c17cc52020-03-03 13:29:12 +0300159 ret_sp->SetBreakpoint(breakpoint);
Martin Svensson0b0dca92019-11-22 11:18:47 +0100160 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() {
Tatyana Krasnukha6c17cc52020-03-03 13:29:12 +0300164 BreakpointSP breakpoint_sp = GetBreakpoint();
165 if (breakpoint_sp) {
166 ProcessSP process_sp = breakpoint_sp->GetTarget().GetProcessSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000167 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(
Tatyana Krasnukha6c17cc52020-03-03 13:29:12 +0300183 breakpoint_sp, m_catch_bp, m_throw_bp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184 }
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) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205 LanguageRuntimeCreateInstance create_callback;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000206 for (uint32_t idx = 0;
207 (create_callback =
208 PluginManager::GetLanguageRuntimeCreateCallbackAtIndex(idx)) !=
209 nullptr;
210 ++idx) {
Jonas Devlieghere99996212020-07-24 15:10:05 -0700211 if (LanguageRuntime *runtime = create_callback(process, language))
212 return runtime;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000213 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000214 return nullptr;
Jim Ingham22777012010-09-23 02:01:19 +0000215}
216
Jonas Devlieghere34d4c8a2020-07-24 16:20:55 -0700217LanguageRuntime::LanguageRuntime(Process *process) : Runtime(process) {}
Jim Ingham22777012010-09-23 02:01:19 +0000218
Alex Langford7f9c9f22019-06-21 19:43:07 +0000219BreakpointPreconditionSP
220LanguageRuntime::GetExceptionPrecondition(LanguageType language,
221 bool throw_bp) {
222 LanguageRuntimeCreateInstance create_callback;
223 for (uint32_t idx = 0;
224 (create_callback =
225 PluginManager::GetLanguageRuntimeCreateCallbackAtIndex(idx)) !=
226 nullptr;
227 idx++) {
228 if (auto precondition_callback =
229 PluginManager::GetLanguageRuntimeGetExceptionPreconditionAtIndex(
230 idx)) {
231 if (BreakpointPreconditionSP precond =
232 precondition_callback(language, throw_bp))
233 return precond;
234 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000235 }
Alex Langford7f9c9f22019-06-21 19:43:07 +0000236 return BreakpointPreconditionSP();
Jim Inghama72b31c2015-04-22 19:42:18 +0000237}
238
Kate Stoneb9c1b512016-09-06 20:57:50 +0000239BreakpointSP LanguageRuntime::CreateExceptionBreakpoint(
240 Target &target, lldb::LanguageType language, bool catch_bp, bool throw_bp,
241 bool is_internal) {
242 BreakpointResolverSP resolver_sp(
243 new ExceptionBreakpointResolver(language, catch_bp, throw_bp));
244 SearchFilterSP filter_sp(
245 new ExceptionSearchFilter(target.shared_from_this(), language));
246 bool hardware = false;
247 bool resolve_indirect_functions = false;
248 BreakpointSP exc_breakpt_sp(
249 target.CreateBreakpoint(filter_sp, resolver_sp, is_internal, hardware,
250 resolve_indirect_functions));
251 if (exc_breakpt_sp) {
Alex Langford7f9c9f22019-06-21 19:43:07 +0000252 if (auto precond = GetExceptionPrecondition(language, throw_bp))
253 exc_breakpt_sp->SetPrecondition(precond);
Jim Inghama72b31c2015-04-22 19:42:18 +0000254
Kate Stoneb9c1b512016-09-06 20:57:50 +0000255 if (is_internal)
256 exc_breakpt_sp->SetBreakpointKind("exception");
257 }
258
259 return exc_breakpt_sp;
Jim Ingham219ba192012-03-05 04:47:34 +0000260}
261
Kate Stoneb9c1b512016-09-06 20:57:50 +0000262void LanguageRuntime::InitializeCommands(CommandObject *parent) {
263 if (!parent)
264 return;
Colin Rileyc9c55a22015-05-04 18:39:38 +0000265
Kate Stoneb9c1b512016-09-06 20:57:50 +0000266 if (!parent->IsMultiwordObject())
267 return;
Colin Rileyc9c55a22015-05-04 18:39:38 +0000268
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269 LanguageRuntimeCreateInstance create_callback;
Colin Rileyc9c55a22015-05-04 18:39:38 +0000270
Kate Stoneb9c1b512016-09-06 20:57:50 +0000271 for (uint32_t idx = 0;
272 (create_callback =
273 PluginManager::GetLanguageRuntimeCreateCallbackAtIndex(idx)) !=
274 nullptr;
275 ++idx) {
276 if (LanguageRuntimeGetCommandObject command_callback =
277 PluginManager::GetLanguageRuntimeGetCommandObjectAtIndex(idx)) {
278 CommandObjectSP command =
279 command_callback(parent->GetCommandInterpreter());
280 if (command) {
281 // the CommandObject vended by a Language plugin cannot be created once
Zachary Turnera4496982016-10-05 21:14:38 +0000282 // and cached because we may create multiple debuggers and need one
283 // instance of the command each - the implementing function is meant to
284 // create a new instance of the command each time it is invoked.
285 parent->LoadSubCommand(command->GetCommandName().str().c_str(), command);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000286 }
Colin Rileyc9c55a22015-05-04 18:39:38 +0000287 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000288 }
Colin Rileyc9c55a22015-05-04 18:39:38 +0000289}