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 
11 #include "lldb/Core/SearchFilter.h"
13 #include "lldb/Target/Language.h"
14 #include "lldb/Target/Target.h"
15 
16 using namespace lldb;
17 using namespace lldb_private;
18 
19 char LanguageRuntime::ID = 0;
20 
21 ExceptionSearchFilter::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)
28 }
29 
30 bool ExceptionSearchFilter::ModulePasses(const lldb::ModuleSP &module_sp) {
32  if (m_filter_sp)
33  return m_filter_sp->ModulePasses(module_sp);
34  return false;
35 }
36 
39  if (m_filter_sp)
40  return m_filter_sp->ModulePasses(spec);
41  return false;
42 }
43 
46  if (m_filter_sp)
47  m_filter_sp->Search(searcher);
48 }
49 
52  if (m_filter_sp)
53  m_filter_sp->GetDescription(s);
54 }
55 
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;
63  } else {
64  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  }
70  }
71 
72  if (refreash_filter && m_language_runtime) {
74  }
75  } else {
76  m_filter_sp.reset();
77  m_language_runtime = nullptr;
78  }
79 }
80 
82  return SearchFilterSP(
83  new ExceptionSearchFilter(TargetSP(), m_language, false));
84 }
85 
87  Target &target, const StructuredData::Dictionary &data_dict,
88  Status &error) {
89  SearchFilter *result = nullptr;
90  return result;
91 }
92 
94  StructuredData::ObjectSP result_sp;
95 
96  return result_sp;
97 }
98 
99 // The Target is the one that knows how to create breakpoints, so this function
100 // is meant to be used either by the target or internally in
101 // Set/ClearExceptionBreakpoints.
103 public:
105  bool throw_bp)
106  : BreakpointResolver(nullptr, BreakpointResolver::ExceptionResolver),
107  m_language(language), m_catch_bp(catch_bp), m_throw_bp(throw_bp) {}
108 
109  ~ExceptionBreakpointResolver() override = default;
110 
112  SymbolContext &context,
113  Address *addr) override {
114 
115  if (SetActualResolver())
116  return m_actual_resolver_sp->SearchCallback(filter, context, addr);
117  else
118  return eCallbackReturnStop;
119  }
120 
122  if (SetActualResolver())
123  return m_actual_resolver_sp->GetDepth();
124  else
126  }
127 
128  void GetDescription(Stream *s) override {
129  Language *language_plugin = Language::FindPlugin(m_language);
130  if (language_plugin)
131  language_plugin->GetExceptionResolverDescription(m_catch_bp, m_throw_bp,
132  *s);
133  else
135  *s);
136 
137  SetActualResolver();
138  if (m_actual_resolver_sp) {
139  s->Printf(" using: ");
140  m_actual_resolver_sp->GetDescription(s);
141  } else
142  s->Printf(" the correct runtime exception handler will be determined "
143  "when you run");
144  }
145 
146  void Dump(Stream *s) const override {}
147 
148  /// Methods for support type inquiry through isa, cast, and dyn_cast:
149  static inline bool classof(const BreakpointResolverName *) { return true; }
150  static inline bool classof(const BreakpointResolver *V) {
152  }
153 
154 protected:
155  BreakpointResolverSP CopyForBreakpoint(BreakpointSP &breakpoint) override {
156  BreakpointResolverSP ret_sp(
157  new ExceptionBreakpointResolver(m_language, m_catch_bp, m_throw_bp));
158  ret_sp->SetBreakpoint(breakpoint);
159  return ret_sp;
160  }
161 
163  BreakpointSP breakpoint_sp = GetBreakpoint();
164  if (breakpoint_sp) {
165  ProcessSP process_sp = breakpoint_sp->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  }
178  }
179 
180  if (refreash_resolver && m_language_runtime) {
181  m_actual_resolver_sp = m_language_runtime->CreateExceptionResolver(
182  breakpoint_sp, 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;
197  LanguageRuntime *m_language_runtime = nullptr;
200 };
201 
203  lldb::LanguageType language) {
204  LanguageRuntimeCreateInstance create_callback;
205  for (uint32_t idx = 0;
206  (create_callback =
208  nullptr;
209  ++idx) {
210  if (LanguageRuntime *runtime = create_callback(process, language))
211  return runtime;
212  }
213  return nullptr;
214 }
215 
217 
218 BreakpointPreconditionSP
220  bool throw_bp) {
221  LanguageRuntimeCreateInstance create_callback;
222  for (uint32_t idx = 0;
223  (create_callback =
225  nullptr;
226  idx++) {
227  if (auto precondition_callback =
229  idx)) {
230  if (BreakpointPreconditionSP precond =
231  precondition_callback(language, throw_bp))
232  return precond;
233  }
234  }
235  return BreakpointPreconditionSP();
236 }
237 
239  Target &target, lldb::LanguageType language, bool catch_bp, bool throw_bp,
240  bool is_internal) {
241  BreakpointResolverSP resolver_sp(
242  new ExceptionBreakpointResolver(language, catch_bp, throw_bp));
243  SearchFilterSP filter_sp(
244  new ExceptionSearchFilter(target.shared_from_this(), language));
245  bool hardware = false;
246  bool resolve_indirect_functions = false;
247  BreakpointSP exc_breakpt_sp(
248  target.CreateBreakpoint(filter_sp, resolver_sp, is_internal, hardware,
249  resolve_indirect_functions));
250  if (exc_breakpt_sp) {
251  if (auto precond = GetExceptionPrecondition(language, throw_bp))
252  exc_breakpt_sp->SetPrecondition(precond);
253 
254  if (is_internal)
255  exc_breakpt_sp->SetBreakpointKind("exception");
256  }
257 
258  return exc_breakpt_sp;
259 }
260 
261 UnwindPlanSP
263  bool &behaves_like_zeroth_frame) {
264  ProcessSP process_sp = thread.GetProcess();
265  if (!process_sp.get())
266  return UnwindPlanSP();
267  if (process_sp->GetDisableLangRuntimeUnwindPlans() == true)
268  return UnwindPlanSP();
269  for (const lldb::LanguageType lang_type : Language::GetSupportedLanguages()) {
270  if (LanguageRuntime *runtime = process_sp->GetLanguageRuntime(lang_type)) {
271  UnwindPlanSP plan_sp = runtime->GetRuntimeUnwindPlan(
272  process_sp, regctx, behaves_like_zeroth_frame);
273  if (plan_sp.get())
274  return plan_sp;
275  }
276  }
277  return UnwindPlanSP();
278 }
279 
281  if (!parent)
282  return;
283 
284  if (!parent->IsMultiwordObject())
285  return;
286 
287  LanguageRuntimeCreateInstance create_callback;
288 
289  for (uint32_t idx = 0;
290  (create_callback =
292  nullptr;
293  ++idx) {
294  if (LanguageRuntimeGetCommandObject command_callback =
296  CommandObjectSP command =
297  command_callback(parent->GetCommandInterpreter());
298  if (command) {
299  // the CommandObject vended by a Language plugin cannot be created once
300  // and cached because we may create multiple debuggers and need one
301  // instance of the command each - the implementing function is meant to
302  // create a new instance of the command each time it is invoked.
303  parent->LoadSubCommand(command->GetCommandName().str().c_str(), command);
304  }
305  }
306  }
307 }
lldb_private::SearchFilter::m_target_sp
lldb::TargetSP m_target_sp
Definition: SearchFilter.h:278
LanguageRuntimeCreateInstance
lldb_private::StructuredData::Dictionary
Definition: StructuredData.h:352
ExceptionBreakpointResolver::SearchCallback
Searcher::CallbackReturn SearchCallback(SearchFilter &filter, SymbolContext &context, Address *addr) override
Definition: LanguageRuntime.cpp:111
lldb::SearchDepth
SearchDepth
Definition: lldb-enumerations.h:283
ExceptionBreakpointResolver::GetDepth
lldb::SearchDepth GetDepth() override
Definition: LanguageRuntime.cpp:121
lldb_private::ExceptionSearchFilter::Search
void Search(Searcher &searcher) override
Call this method to do the search using the Searcher.
Definition: LanguageRuntime.cpp:44
lldb_private::LanguageRuntime::FindPlugin
static LanguageRuntime * FindPlugin(Process *process, lldb::LanguageType language)
Definition: LanguageRuntime.cpp:202
lldb::eSearchDepthTarget
@ eSearchDepthTarget
Definition: lldb-enumerations.h:285
ExceptionBreakpointResolver::ExceptionBreakpointResolver
ExceptionBreakpointResolver(lldb::LanguageType language, bool catch_bp, bool throw_bp)
Definition: LanguageRuntime.cpp:104
lldb_private::ExceptionSearchFilter::SerializeToStructuredData
StructuredData::ObjectSP SerializeToStructuredData() override
Definition: LanguageRuntime.cpp:93
lldb_private::LanguageRuntime::CreateExceptionBreakpoint
static lldb::BreakpointSP CreateExceptionBreakpoint(Target &target, lldb::LanguageType language, bool catch_bp, bool throw_bp, bool is_internal=false)
Definition: LanguageRuntime.cpp:238
lldb_private::Process
Definition: Process.h:338
lldb_private::Language::GetExceptionResolverDescription
virtual void GetExceptionResolverDescription(bool catch_on, bool throw_on, Stream &s)
Definition: Language.cpp:458
lldb::LanguageType
LanguageType
Programming language type.
Definition: lldb-enumerations.h:436
lldb_private::CommandObject::LoadSubCommand
virtual bool LoadSubCommand(llvm::StringRef cmd_name, const lldb::CommandObjectSP &command_obj)
Definition: CommandObject.h:184
lldb_private::PluginManager::GetLanguageRuntimeGetExceptionPreconditionAtIndex
static LanguageRuntimeGetExceptionPrecondition GetLanguageRuntimeGetExceptionPreconditionAtIndex(uint32_t idx)
Definition: PluginManager.cpp:582
lldb_private::Stream
Definition: Stream.h:28
lldb_private::Target::CreateBreakpoint
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:345
lldb_private::LanguageRuntime::CreateExceptionResolver
virtual lldb::BreakpointResolverSP CreateExceptionResolver(const lldb::BreakpointSP &bkpt, bool catch_bp, bool throw_bp)=0
Language.h
lldb_private::ExceptionSearchFilter::ExceptionSearchFilter
ExceptionSearchFilter(const lldb::TargetSP &target_sp, lldb::LanguageType language, bool update_module_list=true)
Definition: LanguageRuntime.cpp:21
lldb_private::SymbolContext
Definition: SymbolContext.h:33
lldb_private::Target
Definition: Target.h:464
ExceptionBreakpointResolver::m_language
lldb::LanguageType m_language
Definition: LanguageRuntime.cpp:196
ExceptionBreakpointResolver::SetActualResolver
bool SetActualResolver()
Definition: LanguageRuntime.cpp:162
Target.h
lldb_private::LanguageRuntime::CreateExceptionSearchFilter
virtual lldb::SearchFilterSP CreateExceptionSearchFilter()
Definition: LanguageRuntime.h:135
ExceptionBreakpointResolver
Definition: LanguageRuntime.cpp:102
lldb_private::Thread::GetProcess
lldb::ProcessSP GetProcess() const
Definition: Thread.h:153
lldb_private::FileSpec
Definition: FileSpec.h:56
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::LanguageRuntime::GetRuntimeUnwindPlan
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...
Definition: LanguageRuntime.cpp:262
lldb_private::ExceptionSearchFilter::UpdateModuleListIfNeeded
void UpdateModuleListIfNeeded()
Definition: LanguageRuntime.cpp:56
ID
static char ID
Definition: IRDynamicChecks.cpp:33
lldb_private::ExceptionSearchFilter::m_language
lldb::LanguageType m_language
Definition: LanguageRuntime.h:51
lldb_private::SearchFilter
General Outline: Provides the callback and search depth for the SearchFilter search.
Definition: SearchFilter.h:83
ExceptionBreakpointResolver::m_throw_bp
bool m_throw_bp
Definition: LanguageRuntime.cpp:199
SearchFilter.h
lldb_private::Thread
Definition: Thread.h:61
lldb_private::StructuredData::ObjectSP
std::shared_ptr< Object > ObjectSP
Definition: StructuredData.h:59
lldb_private::BreakpointResolver::ExceptionResolver
@ ExceptionResolver
Definition: BreakpointResolver.h:133
lldb_private::LanguageRuntime::GetExceptionPrecondition
static lldb::BreakpointPreconditionSP GetExceptionPrecondition(lldb::LanguageType language, bool throw_bp)
Definition: LanguageRuntime.cpp:219
lldb_private::SearchFilter::FilterTy
FilterTy
Definition: SearchFilter.h:217
lldb_private::ExceptionSearchFilter::ModulePasses
bool ModulePasses(const lldb::ModuleSP &module_sp) override
Call this method with a Module to see if that module passes the filter.
Definition: LanguageRuntime.cpp:30
lldb_private::Searcher::CallbackReturn
CallbackReturn
Definition: SearchFilter.h:44
lldb_private::LanguageRuntime::InitializeCommands
static void InitializeCommands(CommandObject *parent)
Definition: LanguageRuntime.cpp:280
ExceptionBreakpointResolver::m_catch_bp
bool m_catch_bp
Definition: LanguageRuntime.cpp:198
lldb_private::ExceptionSearchFilter
Definition: LanguageRuntime.h:27
lldb_private::ExceptionSearchFilter::DoCreateCopy
lldb::SearchFilterSP DoCreateCopy() override
Definition: LanguageRuntime.cpp:81
ExceptionBreakpointResolver::classof
static bool classof(const BreakpointResolver *V)
Definition: LanguageRuntime.cpp:150
ExceptionBreakpointResolver::Dump
void Dump(Stream *s) const override
Standard "Dump" method. At present it does nothing.
Definition: LanguageRuntime.cpp:146
lldb_private::LanguageRuntime
Definition: LanguageRuntime.h:60
lldb_private::WindowsLog::Exception
@ Exception
lldb_private::Runtime
Definition: Runtime.h:15
lldb_private::RegisterContext
Definition: RegisterContext.h:17
lldb_private::Language::FindPlugin
static Language * FindPlugin(lldb::LanguageType language)
Definition: Language.cpp:53
lldb_private::CommandObject
Definition: CommandObject.h:67
lldb_private::BreakpointResolver
General Outline: The BreakpointResolver is a Searcher.
Definition: BreakpointResolver.h:35
lldb_private::Language::GetDefaultExceptionResolverDescription
static void GetDefaultExceptionResolverDescription(bool catch_on, bool throw_on, Stream &s)
Definition: Language.cpp:463
lldb_private::Status
Definition: Status.h:44
uint32_t
lldb_private::Language
Definition: Language.h:29
lldb_private::CommandObject::IsMultiwordObject
virtual bool IsMultiwordObject()
Definition: CommandObject.h:146
lldb_private::Address
Definition: Address.h:59
ExceptionBreakpointResolver::m_actual_resolver_sp
lldb::BreakpointResolverSP m_actual_resolver_sp
Definition: LanguageRuntime.cpp:195
lldb_private::BreakpointResolverName
Definition: BreakpointResolverName.h:24
PluginManager.h
lldb_private::ExceptionSearchFilter::GetDescription
void GetDescription(Stream *s) override
Prints a canonical description for the search filter to the stream s.
Definition: LanguageRuntime.cpp:50
lldb_private::CommandObject::GetCommandInterpreter
CommandInterpreter & GetCommandInterpreter()
Definition: CommandObject.h:124
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
LanguageRuntime.h
lldb_private::Language::GetSupportedLanguages
static std::set< lldb::LanguageType > GetSupportedLanguages()
Definition: Language.cpp:349
lldb_private::PluginManager::GetLanguageRuntimeGetCommandObjectAtIndex
static LanguageRuntimeGetCommandObject GetLanguageRuntimeGetCommandObjectAtIndex(uint32_t idx)
Definition: PluginManager.cpp:574
lldb_private::ExceptionSearchFilter::m_filter_sp
lldb::SearchFilterSP m_filter_sp
Definition: LanguageRuntime.h:53
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
ExceptionBreakpointResolver::classof
static bool classof(const BreakpointResolverName *)
Methods for support type inquiry through isa, cast, and dyn_cast:
Definition: LanguageRuntime.cpp:149
CommandInterpreter.h
lldb_private::LanguageRuntime::LanguageRuntime
LanguageRuntime(Process *process)
Definition: LanguageRuntime.cpp:216
lldb_private::Searcher
General Outline: Provides the callback and search depth for the SearchFilter search.
Definition: SearchFilter.h:42
ExceptionBreakpointResolver::GetDescription
void GetDescription(Stream *s) override
Prints a canonical description for the breakpoint to the stream s.
Definition: LanguageRuntime.cpp:128
lldb_private::ExceptionSearchFilter::CreateFromStructuredData
static SearchFilter * CreateFromStructuredData(Target &target, const StructuredData::Dictionary &data_dict, Status &error)
Definition: LanguageRuntime.cpp:86
lldb
Definition: SBAddress.h:15
ExceptionBreakpointResolver::CopyForBreakpoint
BreakpointResolverSP CopyForBreakpoint(BreakpointSP &breakpoint) override
Definition: LanguageRuntime.cpp:155
lldb_private::ExceptionSearchFilter::m_language_runtime
LanguageRuntime * m_language_runtime
Definition: LanguageRuntime.h:52
lldb_private::PluginManager::GetLanguageRuntimeCreateCallbackAtIndex
static LanguageRuntimeCreateInstance GetLanguageRuntimeCreateCallbackAtIndex(uint32_t idx)
Definition: PluginManager.cpp:569
lldb_private::BreakpointResolver::getResolverID
unsigned getResolverID() const
getResolverID - Return an ID for the concrete type of this object.
Definition: BreakpointResolver.h:145