LLDB mainline
LanguageRuntime.cpp
Go to the documentation of this file.
1//===-- LanguageRuntime.cpp -----------------------------------------------===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8
14#include "lldb/Target/Process.h"
16#include "lldb/Target/Target.h"
17
18using namespace lldb;
19using namespace lldb_private;
20
21char LanguageRuntime::ID = 0;
22
24 lldb::LanguageType language,
25 bool update_module_list)
26 : SearchFilter(target_sp, FilterTy::Exception), m_language(language),
28 if (update_module_list)
30}
31
34 if (m_filter_sp)
35 return m_filter_sp->ModulePasses(module_sp);
36 return false;
37}
38
41 if (m_filter_sp)
42 return m_filter_sp->ModulePasses(spec);
43 return false;
44}
45
48 if (m_filter_sp)
49 m_filter_sp->Search(searcher);
50}
51
57
59 ProcessSP process_sp(m_target_sp->GetProcessSP());
60 if (process_sp) {
61 bool refreash_filter = !m_filter_sp;
62 if (m_language_runtime == nullptr) {
63 m_language_runtime = process_sp->GetLanguageRuntime(m_language);
64 refreash_filter = true;
65 } else {
66 LanguageRuntime *language_runtime =
67 process_sp->GetLanguageRuntime(m_language);
68 if (m_language_runtime != language_runtime) {
69 m_language_runtime = language_runtime;
70 refreash_filter = true;
71 }
72 }
73
74 if (refreash_filter && m_language_runtime) {
75 m_filter_sp = m_language_runtime->CreateExceptionSearchFilter();
76 }
77 } else {
78 m_filter_sp.reset();
79 m_language_runtime = nullptr;
80 }
81}
82
83std::unique_ptr<SearchFilter> ExceptionSearchFilter::DoCreateCopy() {
84 return std::make_unique<ExceptionSearchFilter>(TargetSP(), m_language, false);
85}
86
88 Target &target, const StructuredData::Dictionary &data_dict,
89 Status &error) {
90 SearchFilter *result = nullptr;
91 return result;
92}
93
99
100// The Target is the one that knows how to create breakpoints, so this function
101// is meant to be used either by the target or internally in
102// Set/ClearExceptionBreakpoints.
104public:
106 bool throw_bp)
108 m_language(language), m_catch_bp(catch_bp), m_throw_bp(throw_bp) {}
109
110 ~ExceptionBreakpointResolver() override = default;
111
113 SymbolContext &context,
114 Address *addr) override {
115
116 if (SetActualResolver())
117 return m_actual_resolver_sp->SearchCallback(filter, context, addr);
118 else
119 return eCallbackReturnStop;
120 }
121
123 if (SetActualResolver())
124 return m_actual_resolver_sp->GetDepth();
125 else
127 }
128
129 void GetDescription(Stream *s) override {
130 Language *language_plugin = Language::FindPlugin(m_language);
131 if (language_plugin)
133 *s);
134 else
136 *s);
137
140 s->PutCString(" 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) {
153 }
154
155protected:
159 ret_sp->SetBreakpoint(breakpoint);
160 return ret_sp;
161 }
162
164 BreakpointSP breakpoint_sp = GetBreakpoint();
165 if (breakpoint_sp) {
166 ProcessSP process_sp = breakpoint_sp->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 }
179 }
180
181 if (refreash_resolver && m_language_runtime) {
182 m_actual_resolver_sp = m_language_runtime->CreateExceptionResolver(
183 breakpoint_sp, 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
201};
202
204 lldb::LanguageType language) {
206 if (LanguageRuntime *runtime = cbs.create_callback(process, language))
207 return runtime;
208 }
209 return nullptr;
210}
211
213
215 lldb::StackFrameSP callee_frame_sp) {
216 if (!callee_frame_sp)
217 return;
218 lldb::ProcessSP process_sp = callee_frame_sp->CalculateProcess();
219 if (!process_sp)
220 return;
221 m_runtime = process_sp->GetLanguageRuntime(
222 callee_frame_sp->GuessLanguage().AsLanguageType());
223 if (!m_runtime)
224 return;
225 m_location = m_runtime->GetThrownErrorLocation(*callee_frame_sp);
226}
227
228std::optional<lldb::ValueObjectSP>
230 lldb::StackFrameSP caller_frame_sp) const {
231 if (!caller_frame_sp || !m_runtime || !m_location)
232 return std::nullopt;
233 return m_runtime->GetThrownError(*caller_frame_sp, *m_location);
234}
235
238 bool throw_bp) {
240 if (cbs.precondition_callback) {
241 if (BreakpointPreconditionSP precond =
242 cbs.precondition_callback(language, throw_bp))
243 return precond;
244 }
245 }
247}
248
250 Target &target, lldb::LanguageType language, bool catch_bp, bool throw_bp,
251 bool is_internal) {
252 BreakpointResolverSP resolver_sp(
253 new ExceptionBreakpointResolver(language, catch_bp, throw_bp));
254 SearchFilterSP filter_sp(
255 new ExceptionSearchFilter(target.shared_from_this(), language));
256 bool hardware = false;
257 bool resolve_indirect_functions = false;
258 BreakpointSP exc_breakpt_sp(
259 target.CreateBreakpoint(filter_sp, resolver_sp, is_internal, hardware,
260 resolve_indirect_functions));
261 if (exc_breakpt_sp) {
262 if (auto precond = GetExceptionPrecondition(language, throw_bp))
263 exc_breakpt_sp->SetPrecondition(precond);
264
265 if (is_internal)
266 exc_breakpt_sp->SetBreakpointKind("exception");
267 }
268
269 return exc_breakpt_sp;
270}
271
274 bool &behaves_like_zeroth_frame) {
275 ProcessSP process_sp = thread.GetProcess();
276 if (!process_sp.get())
277 return UnwindPlanSP();
278 if (process_sp->GetDisableLangRuntimeUnwindPlans() == true)
279 return UnwindPlanSP();
280 for (const lldb::LanguageType lang_type : Language::GetSupportedLanguages()) {
281 if (LanguageRuntime *runtime = process_sp->GetLanguageRuntime(lang_type)) {
282 UnwindPlanSP plan_sp = runtime->GetRuntimeUnwindPlan(
283 process_sp, regctx, behaves_like_zeroth_frame);
284 if (plan_sp.get())
285 return plan_sp;
286 }
287 }
288 return UnwindPlanSP();
289}
290
292 const StackID &rhs) {
293 for (LanguageRuntime *runtime : process.GetLanguageRuntimes()) {
294 CFAComparison comparison = runtime->CompareCFAs(lhs, rhs);
295 if (comparison != CFAComparison::NoOpinion)
296 return comparison;
297 }
299}
300
305
307 if (!parent)
308 return;
309
310 if (!parent->IsMultiwordObject())
311 return;
312
314 if (cbs.command_callback) {
315 CommandObjectSP command =
316 cbs.command_callback(parent->GetCommandInterpreter());
317 if (command) {
318 // the CommandObject vended by a Language plugin cannot be created once
319 // and cached because we may create multiple debuggers and need one
320 // instance of the command each - the implementing function is meant to
321 // create a new instance of the command each time it is invoked.
322 parent->LoadSubCommand(command->GetCommandName().str().c_str(), command);
323 }
324 }
325 }
326}
static llvm::raw_ostream & error(Stream &strm)
static bool classof(const BreakpointResolver *V)
void GetDescription(Stream *s) override
Prints a canonical description for the breakpoint to the stream s.
static bool classof(const BreakpointResolverName *)
Methods for support type inquiry through isa, cast, and dyn_cast:
lldb::BreakpointResolverSP m_actual_resolver_sp
LanguageRuntime * m_language_runtime
ExceptionBreakpointResolver(lldb::LanguageType language, bool catch_bp, bool throw_bp)
lldb::SearchDepth GetDepth() override
BreakpointResolverSP CopyForBreakpoint(BreakpointSP &breakpoint) override
Searcher::CallbackReturn SearchCallback(SearchFilter &filter, SymbolContext &context, Address *addr) override
void Dump(Stream *s) const override
Standard "Dump" method. At present it does nothing.
~ExceptionBreakpointResolver() override=default
A section + offset based address class.
Definition Address.h:62
"lldb/Breakpoint/BreakpointResolverName.h" This class sets breakpoints on a given function name,...
lldb::BreakpointSP GetBreakpoint() const
This gets the breakpoint for this resolver.
unsigned getResolverID() const
getResolverID - Return an ID for the concrete type of this object.
BreakpointResolver(const lldb::BreakpointSP &bkpt, unsigned char resolverType, lldb::addr_t offset=0, bool offset_is_insn_count=false)
The breakpoint resolver need to have a breakpoint for "ResolveBreakpoint to make sense.
CommandInterpreter & GetCommandInterpreter()
virtual bool LoadSubCommand(llvm::StringRef cmd_name, const lldb::CommandObjectSP &command_obj)
static SearchFilter * CreateFromStructuredData(Target &target, const StructuredData::Dictionary &data_dict, Status &error)
bool ModulePasses(const lldb::ModuleSP &module_sp) override
Call this method with a Module to see if that module passes the filter.
ExceptionSearchFilter(const lldb::TargetSP &target_sp, lldb::LanguageType language, bool update_module_list=true)
void Search(Searcher &searcher) override
Call this method to do the search using the Searcher.
std::unique_ptr< SearchFilter > DoCreateCopy() override
StructuredData::ObjectSP SerializeToStructuredData() override
void GetDescription(Stream *s) override
Prints a canonical description for the search filter to the stream s.
A file utility class.
Definition FileSpec.h:56
std::optional< lldb::ValueObjectSP > GetThrownError(lldb::StackFrameSP caller_frame_sp) const
See LanguageRuntime::GetThrownError.
ThrownErrorLocation(lldb::StackFrameSP callee_frame_sp=nullptr)
virtual StructuredData::ObjectSP GetLanguageSpecificData(SymbolContext sc)
Language runtime plugins can use this API to report language-specific runtime information about this ...
static CFAComparison CompareCFAs(Process &process, const StackID &lhs, const StackID &rhs)
Compares the frames identified by lhs and rhs, if a language runtime of process can order them.
static LanguageRuntime * FindPlugin(Process *process, lldb::LanguageType language)
static lldb::UnwindPlanSP GetRuntimeUnwindPlan(lldb_private::Thread &thread, lldb_private::RegisterContext *regctx, bool &behaves_like_zeroth_frame)
A language runtime may be able to provide a special UnwindPlan for the frame represented by the regis...
static lldb::BreakpointSP CreateExceptionBreakpoint(Target &target, lldb::LanguageType language, bool catch_bp, bool throw_bp, bool is_internal=false)
static void InitializeCommands(CommandObject *parent)
static lldb::BreakpointPreconditionSP GetExceptionPrecondition(lldb::LanguageType language, bool throw_bp)
static Language * FindPlugin(lldb::LanguageType language)
Definition Language.cpp:61
static void GetDefaultExceptionResolverDescription(bool catch_on, bool throw_on, Stream &s)
Definition Language.cpp:573
virtual void GetExceptionResolverDescription(bool catch_on, bool throw_on, Stream &s)
Definition Language.cpp:568
static std::set< lldb::LanguageType > GetSupportedLanguages()
Definition Language.cpp:463
static llvm::SmallVector< LanguageRuntimeCallbacks > GetLanguageRuntimeCallbacks()
A plug-in interface definition class for debugging a process.
Definition Process.h:370
std::vector< LanguageRuntime * > GetLanguageRuntimes()
Definition Process.cpp:1536
Runtime(Process *process)
Definition Runtime.h:17
General Outline: Provides the callback and search depth for the SearchFilter search.
SearchFilter(const lldb::TargetSP &target_sp)
The basic constructor takes a Target, which gives the space to search.
General Outline: Provides the callback and search depth for the SearchFilter search.
An error handling class.
Definition Status.h:118
A stream class that can stream formatted output to a file.
Definition Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition Stream.cpp:63
std::shared_ptr< Object > ObjectSP
Defines a symbol context baton that can be handed other debug core functions.
lldb::BreakpointSP CreateBreakpoint(const FileSpecList *containingModules, const FileSpec &file, uint32_t line_no, uint32_t column, lldb::addr_t offset, LazyBool check_inlines, LazyBool skip_prologue, bool internal, bool request_hardware, LazyBool move_to_nearest_code)
Definition Target.cpp:528
A class that represents a running process on the host machine.
CFAComparison
How the call frame addresses of two frames order them.
Definition StackID.h:19
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
std::shared_ptr< lldb_private::SearchFilter > SearchFilterSP
std::shared_ptr< lldb_private::BreakpointResolver > BreakpointResolverSP
std::shared_ptr< lldb_private::CommandObject > CommandObjectSP
LanguageType
Programming language type.
std::shared_ptr< lldb_private::Breakpoint > BreakpointSP
std::shared_ptr< lldb_private::Process > ProcessSP
std::shared_ptr< lldb_private::BreakpointPrecondition > BreakpointPreconditionSP
std::shared_ptr< lldb_private::UnwindPlan > UnwindPlanSP
std::shared_ptr< lldb_private::Target > TargetSP
std::shared_ptr< lldb_private::Module > ModuleSP