blob: eabb124d1df2f5cbbb8a56639b338f1c18ebf8e8 [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
81SearchFilterSP
82ExceptionSearchFilter::DoCopyForBreakpoint(Breakpoint &breakpoint) {
83 return SearchFilterSP(
84 new ExceptionSearchFilter(TargetSP(), m_language, false));
85}
86
87SearchFilter *ExceptionSearchFilter::CreateFromStructuredData(
Zachary Turner97206d52017-05-12 04:51:55 +000088 Target &target, const StructuredData::Dictionary &data_dict,
89 Status &error) {
Jim Inghame14dc262016-09-12 23:10:56 +000090 SearchFilter *result = nullptr;
91 return result;
92}
93
94StructuredData::ObjectSP ExceptionSearchFilter::SerializeToStructuredData() {
95 StructuredData::ObjectSP result_sp;
96
97 return result_sp;
98}
Greg Claytonbff78252013-03-11 18:42:51 +000099
100// The Target is the one that knows how to create breakpoints, so this function
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101// is meant to be used either by the target or internally in
102// Set/ClearExceptionBreakpoints.
103class ExceptionBreakpointResolver : public BreakpointResolver {
Greg Claytonbff78252013-03-11 18:42:51 +0000104public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105 ExceptionBreakpointResolver(lldb::LanguageType language, bool catch_bp,
106 bool throw_bp)
107 : BreakpointResolver(nullptr, BreakpointResolver::ExceptionResolver),
108 m_language(language), m_language_runtime(nullptr), m_catch_bp(catch_bp),
109 m_throw_bp(throw_bp) {}
Greg Claytonbff78252013-03-11 18:42:51 +0000110
Kate Stoneb9c1b512016-09-06 20:57:50 +0000111 ~ExceptionBreakpointResolver() override = default;
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000112
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113 Searcher::CallbackReturn SearchCallback(SearchFilter &filter,
Raphael Isemann95e264f2019-10-10 11:26:51 +0000114 SymbolContext &context,
115 Address *addr) override {
Greg Claytonbff78252013-03-11 18:42:51 +0000116
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117 if (SetActualResolver())
Raphael Isemann95e264f2019-10-10 11:26:51 +0000118 return m_actual_resolver_sp->SearchCallback(filter, context, addr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119 else
120 return eCallbackReturnStop;
121 }
122
Jim Ingham4911d362018-09-07 18:43:04 +0000123 lldb::SearchDepth GetDepth() override {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124 if (SetActualResolver())
125 return m_actual_resolver_sp->GetDepth();
126 else
Jim Ingham4911d362018-09-07 18:43:04 +0000127 return lldb::eSearchDepthTarget;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128 }
129
130 void GetDescription(Stream *s) override {
131 Language *language_plugin = Language::FindPlugin(m_language);
132 if (language_plugin)
133 language_plugin->GetExceptionResolverDescription(m_catch_bp, m_throw_bp,
134 *s);
135 else
136 Language::GetDefaultExceptionResolverDescription(m_catch_bp, m_throw_bp,
137 *s);
138
139 SetActualResolver();
140 if (m_actual_resolver_sp) {
141 s->Printf(" using: ");
142 m_actual_resolver_sp->GetDescription(s);
143 } else
144 s->Printf(" the correct runtime exception handler will be determined "
145 "when you run");
146 }
147
148 void Dump(Stream *s) const override {}
149
150 /// Methods for support type inquiry through isa, cast, and dyn_cast:
151 static inline bool classof(const BreakpointResolverName *) { return true; }
152 static inline bool classof(const BreakpointResolver *V) {
153 return V->getResolverID() == BreakpointResolver::ExceptionResolver;
154 }
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000155
Greg Claytonbff78252013-03-11 18:42:51 +0000156protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157 BreakpointResolverSP CopyForBreakpoint(Breakpoint &breakpoint) override {
Martin Svensson0b0dca92019-11-22 11:18:47 +0100158 BreakpointResolverSP ret_sp(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000159 new ExceptionBreakpointResolver(m_language, m_catch_bp, m_throw_bp));
Martin Svensson0b0dca92019-11-22 11:18:47 +0100160 ret_sp->SetBreakpoint(&breakpoint);
161 return ret_sp;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162 }
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
Alex Langford7f9c9f22019-06-21 19:43:07 +0000227BreakpointPreconditionSP
228LanguageRuntime::GetExceptionPrecondition(LanguageType language,
229 bool throw_bp) {
230 LanguageRuntimeCreateInstance create_callback;
231 for (uint32_t idx = 0;
232 (create_callback =
233 PluginManager::GetLanguageRuntimeCreateCallbackAtIndex(idx)) !=
234 nullptr;
235 idx++) {
236 if (auto precondition_callback =
237 PluginManager::GetLanguageRuntimeGetExceptionPreconditionAtIndex(
238 idx)) {
239 if (BreakpointPreconditionSP precond =
240 precondition_callback(language, throw_bp))
241 return precond;
242 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000243 }
Alex Langford7f9c9f22019-06-21 19:43:07 +0000244 return BreakpointPreconditionSP();
Jim Inghama72b31c2015-04-22 19:42:18 +0000245}
246
Kate Stoneb9c1b512016-09-06 20:57:50 +0000247BreakpointSP LanguageRuntime::CreateExceptionBreakpoint(
248 Target &target, lldb::LanguageType language, bool catch_bp, bool throw_bp,
249 bool is_internal) {
250 BreakpointResolverSP resolver_sp(
251 new ExceptionBreakpointResolver(language, catch_bp, throw_bp));
252 SearchFilterSP filter_sp(
253 new ExceptionSearchFilter(target.shared_from_this(), language));
254 bool hardware = false;
255 bool resolve_indirect_functions = false;
256 BreakpointSP exc_breakpt_sp(
257 target.CreateBreakpoint(filter_sp, resolver_sp, is_internal, hardware,
258 resolve_indirect_functions));
259 if (exc_breakpt_sp) {
Alex Langford7f9c9f22019-06-21 19:43:07 +0000260 if (auto precond = GetExceptionPrecondition(language, throw_bp))
261 exc_breakpt_sp->SetPrecondition(precond);
Jim Inghama72b31c2015-04-22 19:42:18 +0000262
Kate Stoneb9c1b512016-09-06 20:57:50 +0000263 if (is_internal)
264 exc_breakpt_sp->SetBreakpointKind("exception");
265 }
266
267 return exc_breakpt_sp;
Jim Ingham219ba192012-03-05 04:47:34 +0000268}
269
Kate Stoneb9c1b512016-09-06 20:57:50 +0000270void LanguageRuntime::InitializeCommands(CommandObject *parent) {
271 if (!parent)
272 return;
Colin Rileyc9c55a22015-05-04 18:39:38 +0000273
Kate Stoneb9c1b512016-09-06 20:57:50 +0000274 if (!parent->IsMultiwordObject())
275 return;
Colin Rileyc9c55a22015-05-04 18:39:38 +0000276
Kate Stoneb9c1b512016-09-06 20:57:50 +0000277 LanguageRuntimeCreateInstance create_callback;
Colin Rileyc9c55a22015-05-04 18:39:38 +0000278
Kate Stoneb9c1b512016-09-06 20:57:50 +0000279 for (uint32_t idx = 0;
280 (create_callback =
281 PluginManager::GetLanguageRuntimeCreateCallbackAtIndex(idx)) !=
282 nullptr;
283 ++idx) {
284 if (LanguageRuntimeGetCommandObject command_callback =
285 PluginManager::GetLanguageRuntimeGetCommandObjectAtIndex(idx)) {
286 CommandObjectSP command =
287 command_callback(parent->GetCommandInterpreter());
288 if (command) {
289 // the CommandObject vended by a Language plugin cannot be created once
Zachary Turnera4496982016-10-05 21:14:38 +0000290 // and cached because we may create multiple debuggers and need one
291 // instance of the command each - the implementing function is meant to
292 // create a new instance of the command each time it is invoked.
293 parent->LoadSubCommand(command->GetCommandName().str().c_str(), command);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000294 }
Colin Rileyc9c55a22015-05-04 18:39:38 +0000295 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000296 }
Colin Rileyc9c55a22015-05-04 18:39:38 +0000297}