blob: 999ac99e93c35551c09e9bc5f1d4503489df882d [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/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 {
158 return BreakpointResolverSP(
159 new ExceptionBreakpointResolver(m_language, m_catch_bp, m_throw_bp));
160 }
Jim Ingham33df7cd2014-12-06 01:28:03 +0000161
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162 bool SetActualResolver() {
163 ProcessSP process_sp;
164 if (m_breakpoint) {
165 process_sp = m_breakpoint->GetTarget().GetProcessSP();
166 if (process_sp) {
167 bool refreash_resolver = !m_actual_resolver_sp;
168 if (m_language_runtime == nullptr) {
169 m_language_runtime = process_sp->GetLanguageRuntime(m_language);
170 refreash_resolver = true;
171 } else {
172 LanguageRuntime *language_runtime =
173 process_sp->GetLanguageRuntime(m_language);
174 if (m_language_runtime != language_runtime) {
175 m_language_runtime = language_runtime;
176 refreash_resolver = true;
177 }
Greg Claytonbff78252013-03-11 18:42:51 +0000178 }
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000179
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180 if (refreash_resolver && m_language_runtime) {
181 m_actual_resolver_sp = m_language_runtime->CreateExceptionResolver(
182 m_breakpoint, m_catch_bp, m_throw_bp);
183 }
184 } else {
185 m_actual_resolver_sp.reset();
186 m_language_runtime = nullptr;
187 }
188 } else {
189 m_actual_resolver_sp.reset();
190 m_language_runtime = nullptr;
191 }
192 return (bool)m_actual_resolver_sp;
193 }
194
195 lldb::BreakpointResolverSP m_actual_resolver_sp;
196 lldb::LanguageType m_language;
197 LanguageRuntime *m_language_runtime;
198 bool m_catch_bp;
199 bool m_throw_bp;
Greg Claytonbff78252013-03-11 18:42:51 +0000200};
201
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202LanguageRuntime *LanguageRuntime::FindPlugin(Process *process,
203 lldb::LanguageType language) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000204 std::unique_ptr<LanguageRuntime> language_runtime_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205 LanguageRuntimeCreateInstance create_callback;
Jim Ingham22777012010-09-23 02:01:19 +0000206
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207 for (uint32_t idx = 0;
208 (create_callback =
209 PluginManager::GetLanguageRuntimeCreateCallbackAtIndex(idx)) !=
210 nullptr;
211 ++idx) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000212 language_runtime_up.reset(create_callback(process, language));
Jim Ingham22777012010-09-23 02:01:19 +0000213
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000214 if (language_runtime_up)
215 return language_runtime_up.release();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000216 }
Jim Ingham22777012010-09-23 02:01:19 +0000217
Kate Stoneb9c1b512016-09-06 20:57:50 +0000218 return nullptr;
Jim Ingham22777012010-09-23 02:01:19 +0000219}
220
Kate Stoneb9c1b512016-09-06 20:57:50 +0000221LanguageRuntime::LanguageRuntime(Process *process) : m_process(process) {}
Jim Ingham22777012010-09-23 02:01:19 +0000222
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000223LanguageRuntime::~LanguageRuntime() = default;
Jim Ingham219ba192012-03-05 04:47:34 +0000224
Alex Langford7f9c9f22019-06-21 19:43:07 +0000225BreakpointPreconditionSP
226LanguageRuntime::GetExceptionPrecondition(LanguageType language,
227 bool throw_bp) {
228 LanguageRuntimeCreateInstance create_callback;
229 for (uint32_t idx = 0;
230 (create_callback =
231 PluginManager::GetLanguageRuntimeCreateCallbackAtIndex(idx)) !=
232 nullptr;
233 idx++) {
234 if (auto precondition_callback =
235 PluginManager::GetLanguageRuntimeGetExceptionPreconditionAtIndex(
236 idx)) {
237 if (BreakpointPreconditionSP precond =
238 precondition_callback(language, throw_bp))
239 return precond;
240 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000241 }
Alex Langford7f9c9f22019-06-21 19:43:07 +0000242 return BreakpointPreconditionSP();
Jim Inghama72b31c2015-04-22 19:42:18 +0000243}
244
Kate Stoneb9c1b512016-09-06 20:57:50 +0000245BreakpointSP LanguageRuntime::CreateExceptionBreakpoint(
246 Target &target, lldb::LanguageType language, bool catch_bp, bool throw_bp,
247 bool is_internal) {
248 BreakpointResolverSP resolver_sp(
249 new ExceptionBreakpointResolver(language, catch_bp, throw_bp));
250 SearchFilterSP filter_sp(
251 new ExceptionSearchFilter(target.shared_from_this(), language));
252 bool hardware = false;
253 bool resolve_indirect_functions = false;
254 BreakpointSP exc_breakpt_sp(
255 target.CreateBreakpoint(filter_sp, resolver_sp, is_internal, hardware,
256 resolve_indirect_functions));
257 if (exc_breakpt_sp) {
Alex Langford7f9c9f22019-06-21 19:43:07 +0000258 if (auto precond = GetExceptionPrecondition(language, throw_bp))
259 exc_breakpt_sp->SetPrecondition(precond);
Jim Inghama72b31c2015-04-22 19:42:18 +0000260
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261 if (is_internal)
262 exc_breakpt_sp->SetBreakpointKind("exception");
263 }
264
265 return exc_breakpt_sp;
Jim Ingham219ba192012-03-05 04:47:34 +0000266}
267
Kate Stoneb9c1b512016-09-06 20:57:50 +0000268void LanguageRuntime::InitializeCommands(CommandObject *parent) {
269 if (!parent)
270 return;
Colin Rileyc9c55a22015-05-04 18:39:38 +0000271
Kate Stoneb9c1b512016-09-06 20:57:50 +0000272 if (!parent->IsMultiwordObject())
273 return;
Colin Rileyc9c55a22015-05-04 18:39:38 +0000274
Kate Stoneb9c1b512016-09-06 20:57:50 +0000275 LanguageRuntimeCreateInstance create_callback;
Colin Rileyc9c55a22015-05-04 18:39:38 +0000276
Kate Stoneb9c1b512016-09-06 20:57:50 +0000277 for (uint32_t idx = 0;
278 (create_callback =
279 PluginManager::GetLanguageRuntimeCreateCallbackAtIndex(idx)) !=
280 nullptr;
281 ++idx) {
282 if (LanguageRuntimeGetCommandObject command_callback =
283 PluginManager::GetLanguageRuntimeGetCommandObjectAtIndex(idx)) {
284 CommandObjectSP command =
285 command_callback(parent->GetCommandInterpreter());
286 if (command) {
287 // the CommandObject vended by a Language plugin cannot be created once
Zachary Turnera4496982016-10-05 21:14:38 +0000288 // and cached because we may create multiple debuggers and need one
289 // instance of the command each - the implementing function is meant to
290 // create a new instance of the command each time it is invoked.
291 parent->LoadSubCommand(command->GetCommandName().str().c_str(), command);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000292 }
Colin Rileyc9c55a22015-05-04 18:39:38 +0000293 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000294 }
Colin Rileyc9c55a22015-05-04 18:39:38 +0000295}