LLDB mainline
ScriptInterpreter.h
Go to the documentation of this file.
1//===-- ScriptInterpreter.h -------------------------------------*- C++ -*-===//
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
9#ifndef LLDB_INTERPRETER_SCRIPTINTERPRETER_H
10#define LLDB_INTERPRETER_SCRIPTINTERPRETER_H
11
27#include "lldb/Utility/Status.h"
30#include "lldb/lldb-private.h"
31#include <memory>
32#include <optional>
33
34namespace lldb_private {
35
37public:
39
40 virtual ~ScriptInterpreterLocker() = default;
41
42private:
46};
47
49public:
51
52 bool GetEnableIO() const { return m_enable_io; }
53
54 bool GetSetLLDBGlobals() const { return m_set_lldb_globals; }
55
56 // If this is true then any exceptions raised by the script will be
57 // cleared with PyErr_Clear(). If false then they will be left for
58 // the caller to clean up
59 bool GetMaskoutErrors() const { return m_maskout_errors; }
60
62 m_enable_io = enable;
63 return *this;
64 }
65
68 return *this;
69 }
70
72 m_maskout_errors = maskout;
73 return *this;
74 }
75
76private:
77 bool m_enable_io = true;
78 bool m_set_lldb_globals = true;
79 bool m_maskout_errors = true;
80};
81
83public:
84 LoadScriptOptions() = default;
85
86 bool GetInitSession() const { return m_init_session; }
87 bool GetSilent() const { return m_silent; }
88
91 return *this;
92 }
93
95 m_silent = b;
96 return *this;
97 }
98
99private:
100 bool m_init_session = false;
101 bool m_silent = false;
102};
103
105public:
106 /// Create an IO redirect. If IO is enabled, this will redirects the output
107 /// to the command return object if set or to the debugger otherwise. If IO
108 /// is disabled, it will redirect all IO to /dev/null.
109 static llvm::Expected<std::unique_ptr<ScriptInterpreterIORedirect>>
110 Create(bool enable_io, Debugger &debugger, CommandReturnObject *result);
111
113
116 return m_output_file_sp->GetUnlockedFileSP();
117 }
119 return m_error_file_sp->GetUnlockedFileSP();
120 }
121
122 /// Flush our output and error file handles.
123 void Flush();
124
125private:
126 ScriptInterpreterIORedirect(std::unique_ptr<File> input,
127 std::unique_ptr<File> output);
129
136};
137
139public:
157
158 ScriptInterpreter(Debugger &debugger, lldb::ScriptLanguage script_lang);
159
161
162 /// Describes one extension to emit into the generated template file.
163 ///
164 /// `full_name` is the dotted extension name as the user typed it (e.g.
165 /// `"ScriptedProcess"` or a nested `"...ChildKind"`), used to name the
166 /// generated class. `path` is the same name split on `.`; its last element
167 /// selects the ScriptedExtension kind and everything before it is treated
168 /// as a subclass discriminator. Both StringRefs must outlive the call to
169 /// GenerateExtensionTemplate; the implementation stores them, not copies.
171 llvm::StringRef full_name;
172 llvm::SmallVector<llvm::StringRef> path;
173 };
174
175 virtual llvm::Expected<FileSpec>
176 GenerateExtensionTemplate(const std::string &name,
177 std::vector<ExtensionTemplateRequest> &extensions,
178 bool generate_non_abstract_methods,
179 std::string output_file);
180
181 ~ScriptInterpreter() override = default;
182
183 virtual bool Interrupt() { return false; }
184
185 virtual bool ExecuteOneLine(
186 llvm::StringRef command, CommandReturnObject *result,
187 const ExecuteScriptOptions &options = ExecuteScriptOptions()) = 0;
188
189 virtual void ExecuteInterpreterLoop() = 0;
190
192 llvm::StringRef in_string, ScriptReturnType return_type, void *ret_value,
193 const ExecuteScriptOptions &options = ExecuteScriptOptions()) {
194 return true;
195 }
196
198 const char *in_string,
199 const ExecuteScriptOptions &options = ExecuteScriptOptions()) {
200 return Status::FromErrorString("not implemented");
201 }
202
203 virtual Status
205 return Status::FromErrorString("not implemented");
206 }
207
209 std::string &output,
210 bool has_extra_args,
211 bool is_callback) {
212 return Status::FromErrorString("not implemented");
213 }
214
216 std::string &output,
217 bool is_callback) {
218 return false;
219 }
220
221 virtual bool GenerateTypeScriptFunction(const char *oneliner,
222 std::string &output,
223 const void *name_token = nullptr) {
224 return false;
225 }
226
228 std::string &output,
229 const void *name_token = nullptr) {
230 return false;
231 }
232
234 std::string &output) {
235 return false;
236 }
237
238 virtual bool GenerateTypeSynthClass(StringList &input, std::string &output,
239 const void *name_token = nullptr) {
240 return false;
241 }
242
243 virtual bool GenerateTypeSynthClass(const char *oneliner, std::string &output,
244 const void *name_token = nullptr) {
245 return false;
246 }
247
252
255 const char *setting_name, lldb_private::Status &error) {
257 }
258
259 virtual Status GenerateFunction(const char *signature,
260 const StringList &input,
261 bool is_callback) {
262 return Status::FromErrorString("not implemented");
263 }
264
266 std::vector<std::reference_wrapper<BreakpointOptions>> &options,
267 CommandReturnObject &result);
268
269 virtual void
271 CommandReturnObject &result);
272
273 /// Set the specified text as the callback for the breakpoint.
275 std::vector<std::reference_wrapper<BreakpointOptions>> &bp_options_vec,
276 const char *callback_text);
277
279 const char *callback_text,
280 bool is_callback) {
281 return Status::FromErrorString("not implemented");
282 }
283
284 /// This one is for deserialization:
286 BreakpointOptions &bp_options,
287 std::unique_ptr<BreakpointOptions::CommandData> &data_up) {
288 return Status::FromErrorString("not implemented");
289 }
290
292 std::vector<std::reference_wrapper<BreakpointOptions>> &bp_options_vec,
293 const char *function_name, StructuredData::ObjectSP extra_args_sp);
294
295 /// Set a script function as the callback for the breakpoint.
296 virtual Status
298 const char *function_name,
299 StructuredData::ObjectSP extra_args_sp) {
300 return Status::FromErrorString("not implemented");
301 }
302
303 /// Set a one-liner as the callback for the watchpoint.
305 const char *user_input,
306 bool is_callback) {}
307
308 virtual bool GetScriptedSummary(const char *function_name,
309 lldb::ValueObjectSP valobj,
310 StructuredData::ObjectSP &callee_wrapper_sp,
311 const TypeSummaryOptions &options,
312 std::string &retval) {
313 return false;
314 }
315
316 // Calls the specified formatter matching Python function and returns its
317 // result (true if it's a match, false if we should keep looking for a
318 // matching formatter).
319 virtual bool FormatterCallbackFunction(const char *function_name,
320 lldb::TypeImplSP type_impl_sp) {
321 return true;
322 }
323
324 virtual void Clear() {
325 // Clean up any ref counts to SBObjects that might be in global variables
326 }
327
328 virtual bool
329 RunScriptBasedCommand(const char *impl_function, llvm::StringRef args,
330 ScriptedCommandSynchronicity synchronicity,
332 Status &error,
333 const lldb_private::ExecutionContext &exe_ctx) {
334 return false;
335 }
336
337 virtual bool RunScriptFormatKeyword(const char *impl_function,
338 Process *process, std::string &output,
339 Status &error) {
340 error = Status::FromErrorString("unimplemented");
341 return false;
342 }
343
344 virtual bool RunScriptFormatKeyword(const char *impl_function, Thread *thread,
345 std::string &output, Status &error) {
346 error = Status::FromErrorString("unimplemented");
347 return false;
348 }
349
350 virtual bool RunScriptFormatKeyword(const char *impl_function, Target *target,
351 std::string &output, Status &error) {
352 error = Status::FromErrorString("unimplemented");
353 return false;
354 }
355
356 virtual bool RunScriptFormatKeyword(const char *impl_function,
357 StackFrame *frame, std::string &output,
358 Status &error) {
359 error = Status::FromErrorString("unimplemented");
360 return false;
361 }
362
363 virtual bool RunScriptFormatKeyword(const char *impl_function,
364 ValueObject *value, std::string &output,
365 Status &error) {
366 error = Status::FromErrorString("unimplemented");
367 return false;
368 }
369
370 virtual bool GetDocumentationForItem(const char *item, std::string &dest) {
371 dest.clear();
372 return false;
373 }
374
375 virtual bool CheckObjectExists(const char *name) { return false; }
376
377 virtual bool
378 LoadScriptingModule(const char *filename, const LoadScriptOptions &options,
380 StructuredData::ObjectSP *module_sp = nullptr,
381 FileSpec extra_search_dir = {},
382 lldb::TargetSP loaded_into_target_sp = {});
383
387
388 virtual bool IsReservedWord(const char *word) { return false; }
389
390 virtual std::unique_ptr<ScriptInterpreterLocker> AcquireInterpreterLock();
391
393
394 virtual llvm::Expected<unsigned>
395 GetMaxPositionalArgumentsForCallable(const llvm::StringRef &callable_name) {
396 return llvm::createStringError(
397 llvm::inconvertibleErrorCode(), "Unimplemented function");
398 }
399
400 static std::string LanguageToString(lldb::ScriptLanguage language);
401
402 static lldb::ScriptLanguage StringToLanguage(const llvm::StringRef &string);
403
404 virtual llvm::Expected<std::string>
406 return llvm::make_error<UnimplementedError>();
407 }
408
409 static llvm::StringLiteral
411
412 static lldb::ScriptedExtension StringToExtension(llvm::StringRef string);
413
415
419
423
427
430 return {};
431 }
432
435 return {};
436 }
437
441
445
449
452 return {};
453 }
454
459
463
466 return {};
467 }
468
473
478
479 /// Holds an lldb_private::Module name and a "sanitized" version
480 /// of it for the purposes of loading a script of that name by
481 /// the relevant ScriptInterpreter.
482 ///
483 /// E.g., for Python the sanitized name can't include:
484 /// * Special characters: '-', ' ', '.'
485 /// * Python keywords
487 public:
488 SanitizedScriptingModuleName(std::string name, std::string sanitized_name,
489 std::string conflicting_keyword)
490 : m_original_name(std::move(name)),
491 m_sanitized_name(std::move(sanitized_name)),
492 m_conflicting_keyword(std::move(conflicting_keyword)) {}
493
494 /// Returns \c true if this name is a keyword in the associated scripting
495 /// language.
496 bool IsKeyword() const { return !m_conflicting_keyword.empty(); }
497
498 /// Returns \c true if the original name has been sanitized (i.e., required
499 /// changes).
500 bool RequiredSanitization() const {
502 }
503
504 llvm::StringRef GetSanitizedName() const { return m_sanitized_name; }
505 llvm::StringRef GetOriginalName() const { return m_original_name; }
506 llvm::StringRef GetConflictingKeyword() const {
508 }
509
510 private:
511 std::string m_original_name;
512 std::string m_sanitized_name;
513
514 /// If the m_sanitized_name conflicts with a keyword for the
515 /// ScriptInterpreter language associated with this
516 /// SanitizedScriptingModuleName, is set to the conflicting keyword. Empty
517 /// otherwise.
519 };
520
522 GetSanitizedScriptingModuleName(llvm::StringRef name);
523
524 /// Get the debugger associated with this script interpreter.
526 const Debugger &GetDebugger() const { return m_debugger; }
527
528protected:
531
532private:
534 std::make_shared<ScriptedInstanceRegistry>();
535};
536
537} // namespace lldb_private
538
539#endif // LLDB_INTERPRETER_SCRIPTINTERPRETER_H
static llvm::raw_ostream & error(Stream &strm)
"lldb/Breakpoint/BreakpointOptions.h" Class that manages the options on a breakpoint or breakpoint lo...
A class to manage flag bits.
Definition Debugger.h:101
ExecuteScriptOptions & SetMaskoutErrors(bool maskout)
ExecuteScriptOptions & SetSetLLDBGlobals(bool set)
ExecuteScriptOptions & SetEnableIO(bool enable)
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
A file utility class.
Definition FileSpec.h:56
LoadScriptOptions & SetInitSession(bool b)
LoadScriptOptions & SetSilent(bool b)
std::recursive_mutex Mutex
Definition StreamFile.h:77
A plug-in interface definition class for debugging a process.
Definition Process.h:369
void Flush()
Flush our output and error file handles.
ScriptInterpreterIORedirect(std::unique_ptr< File > input, std::unique_ptr< File > output)
static llvm::Expected< std::unique_ptr< ScriptInterpreterIORedirect > > Create(bool enable_io, Debugger &debugger, CommandReturnObject *result)
Create an IO redirect.
const ScriptInterpreterLocker & operator=(const ScriptInterpreterLocker &)=delete
ScriptInterpreterLocker(const ScriptInterpreterLocker &)=delete
virtual ~ScriptInterpreterLocker()=default
Holds an lldb_private::Module name and a "sanitized" version of it for the purposes of loading a scri...
bool RequiredSanitization() const
Returns true if the original name has been sanitized (i.e., required changes).
SanitizedScriptingModuleName(std::string name, std::string sanitized_name, std::string conflicting_keyword)
std::string m_conflicting_keyword
If the m_sanitized_name conflicts with a keyword for the ScriptInterpreter language associated with t...
bool IsKeyword() const
Returns true if this name is a keyword in the associated scripting language.
static lldb::ScriptLanguage StringToLanguage(const llvm::StringRef &string)
virtual lldb::ScriptedHookInterfaceSP CreateScriptedHookInterface()
virtual void CollectDataForBreakpointCommandCallback(std::vector< std::reference_wrapper< BreakpointOptions > > &options, CommandReturnObject &result)
const char * GetScriptInterpreterPtyName()
virtual Status ExecuteMultipleLines(const char *in_string, const ExecuteScriptOptions &options=ExecuteScriptOptions())
virtual lldb::ScriptedSyntheticChildrenInterfaceSP CreateScriptedSyntheticChildrenInterface()
virtual bool GenerateTypeScriptFunction(const char *oneliner, std::string &output, const void *name_token=nullptr)
virtual bool RunScriptFormatKeyword(const char *impl_function, Target *target, std::string &output, Status &error)
virtual bool GenerateTypeScriptFunction(StringList &input, std::string &output, const void *name_token=nullptr)
virtual lldb::ScriptedProcessInterfaceUP CreateScriptedProcessInterface()
virtual lldb::ScriptedFrameInterfaceSP CreateScriptedFrameInterface()
virtual Status ExportFunctionDefinitionToInterpreter(StringList &function_def)
~ScriptInterpreter() override=default
virtual lldb::OperatingSystemInterfaceSP CreateOperatingSystemInterface()
virtual lldb::ScriptedCommandInterfaceSP CreateScriptedCommandInterface()
virtual Status SetBreakpointCommandCallbackFunction(BreakpointOptions &bp_options, const char *function_name, StructuredData::ObjectSP extra_args_sp)
Set a script function as the callback for the breakpoint.
virtual llvm::Expected< unsigned > GetMaxPositionalArgumentsForCallable(const llvm::StringRef &callable_name)
virtual bool ExecuteOneLineWithReturn(llvm::StringRef in_string, ScriptReturnType return_type, void *ret_value, const ExecuteScriptOptions &options=ExecuteScriptOptions())
virtual bool GenerateTypeSynthClass(StringList &input, std::string &output, const void *name_token=nullptr)
virtual bool RunScriptBasedCommand(const char *impl_function, llvm::StringRef args, ScriptedCommandSynchronicity synchronicity, lldb_private::CommandReturnObject &cmd_retobj, Status &error, const lldb_private::ExecutionContext &exe_ctx)
const lldb::ScriptedInstanceRegistrySP & GetScriptedInstanceRegistry() const
virtual llvm::Expected< FileSpec > GenerateExtensionTemplate(const std::string &name, std::vector< ExtensionTemplateRequest > &extensions, bool generate_non_abstract_methods, std::string output_file)
virtual void ExecuteInterpreterLoop()=0
virtual lldb::ScriptedBreakpointInterfaceSP CreateScriptedBreakpointInterface()
virtual bool GenerateScriptAliasFunction(StringList &input, std::string &output)
static llvm::StringLiteral ExtensionToString(lldb::ScriptedExtension extension)
virtual llvm::Expected< std::string > ExtensionToImportPath(lldb::ScriptedExtension extension)
static lldb::ScriptedExtension StringToExtension(llvm::StringRef string)
virtual bool GenerateWatchpointCommandCallbackData(StringList &input, std::string &output, bool is_callback)
virtual StructuredData::DictionarySP GetDynamicSettings(StructuredData::ObjectSP plugin_module_sp, Target *target, const char *setting_name, lldb_private::Status &error)
virtual lldb::ScriptedStringSummaryInterfaceSP CreateScriptedStringSummaryInterface()
virtual Status GenerateFunction(const char *signature, const StringList &input, bool is_callback)
virtual void SetWatchpointCommandCallback(WatchpointOptions *wp_options, const char *user_input, bool is_callback)
Set a one-liner as the callback for the watchpoint.
Status SetBreakpointCommandCallback(std::vector< std::reference_wrapper< BreakpointOptions > > &bp_options_vec, const char *callback_text)
Set the specified text as the callback for the breakpoint.
virtual bool ExecuteOneLine(llvm::StringRef command, CommandReturnObject *result, const ExecuteScriptOptions &options=ExecuteScriptOptions())=0
virtual bool LoadScriptingModule(const char *filename, const LoadScriptOptions &options, lldb_private::Status &error, StructuredData::ObjectSP *module_sp=nullptr, FileSpec extra_search_dir={}, lldb::TargetSP loaded_into_target_sp={})
virtual bool RunScriptFormatKeyword(const char *impl_function, StackFrame *frame, std::string &output, Status &error)
virtual Status GenerateBreakpointCommandCallbackData(StringList &input, std::string &output, bool has_extra_args, bool is_callback)
static std::string LanguageToString(lldb::ScriptLanguage language)
virtual bool RunScriptFormatKeyword(const char *impl_function, ValueObject *value, std::string &output, Status &error)
virtual std::unique_ptr< ScriptInterpreterLocker > AcquireInterpreterLock()
virtual bool GetDocumentationForItem(const char *item, std::string &dest)
virtual StructuredData::DictionarySP GetInterpreterInfo()
virtual StructuredData::ObjectSP LoadPluginModule(const FileSpec &file_spec, lldb_private::Status &error)
Status SetBreakpointCommandCallbackFunction(std::vector< std::reference_wrapper< BreakpointOptions > > &bp_options_vec, const char *function_name, StructuredData::ObjectSP extra_args_sp)
virtual bool GenerateTypeSynthClass(const char *oneliner, std::string &output, const void *name_token=nullptr)
virtual StructuredData::ObjectSP CreateStructuredDataFromScriptObject(ScriptObject obj)
virtual bool GetScriptedSummary(const char *function_name, lldb::ValueObjectSP valobj, StructuredData::ObjectSP &callee_wrapper_sp, const TypeSummaryOptions &options, std::string &retval)
virtual Status SetBreakpointCommandCallback(BreakpointOptions &bp_options, std::unique_ptr< BreakpointOptions::CommandData > &data_up)
This one is for deserialization:
const Debugger & GetDebugger() const
virtual lldb::ScriptedThreadPlanInterfaceSP CreateScriptedThreadPlanInterface()
virtual bool RunScriptFormatKeyword(const char *impl_function, Thread *thread, std::string &output, Status &error)
virtual bool CheckObjectExists(const char *name)
virtual bool FormatterCallbackFunction(const char *function_name, lldb::TypeImplSP type_impl_sp)
lldb::ScriptLanguage GetLanguage()
virtual bool IsReservedWord(const char *word)
lldb::ScriptedInstanceRegistrySP m_instance_registry_sp
virtual lldb::ScriptedPlatformInterfaceUP GetScriptedPlatformInterface()
virtual bool RunScriptFormatKeyword(const char *impl_function, Process *process, std::string &output, Status &error)
virtual lldb::ScriptedThreadInterfaceSP CreateScriptedThreadInterface()
virtual Status SetBreakpointCommandCallback(BreakpointOptions &bp_options, const char *callback_text, bool is_callback)
virtual lldb::ScriptedFrameProviderInterfaceSP CreateScriptedFrameProviderInterface()
ScriptInterpreter(Debugger &debugger, lldb::ScriptLanguage script_lang)
virtual void CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options, CommandReturnObject &result)
virtual SanitizedScriptingModuleName GetSanitizedScriptingModuleName(llvm::StringRef name)
virtual lldb::ScriptedStackFrameRecognizerInterfaceSP CreateScriptedStackFrameRecognizerInterface()
Debugger & GetDebugger()
Get the debugger associated with this script interpreter.
This base class provides an interface to stack frames.
Definition StackFrame.h:44
An error handling class.
Definition Status.h:118
static Status FromErrorString(const char *str)
Definition Status.h:141
std::shared_ptr< Dictionary > DictionarySP
std::shared_ptr< Object > ObjectSP
"lldb/Core/ThreadedCommunication.h" Variation of Communication that supports threaded reads.
"lldb/Breakpoint/WatchpointOptions.h" Class that manages the options on a watchpoint.
A class that represents a running process on the host machine.
std::unique_ptr< lldb_private::ScriptedPlatformInterface > ScriptedPlatformInterfaceUP
ScriptLanguage
Script interpreter types.
ScriptedExtension
Scripting extension types.
std::shared_ptr< lldb_private::ScriptedSyntheticChildrenInterface > ScriptedSyntheticChildrenInterfaceSP
std::shared_ptr< lldb_private::ScriptedHookInterface > ScriptedHookInterfaceSP
std::shared_ptr< lldb_private::ScriptedStringSummaryInterface > ScriptedStringSummaryInterfaceSP
std::shared_ptr< lldb_private::ScriptedThreadPlanInterface > ScriptedThreadPlanInterfaceSP
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
std::shared_ptr< lldb_private::OperatingSystemInterface > OperatingSystemInterfaceSP
std::shared_ptr< lldb_private::ScriptedBreakpointInterface > ScriptedBreakpointInterfaceSP
std::shared_ptr< lldb_private::ScriptedThreadInterface > ScriptedThreadInterfaceSP
std::shared_ptr< lldb_private::ScriptedFrameProviderInterface > ScriptedFrameProviderInterfaceSP
std::shared_ptr< lldb_private::ScriptedCommandInterface > ScriptedCommandInterfaceSP
std::shared_ptr< lldb_private::LockableStreamFile > LockableStreamFileSP
std::shared_ptr< lldb_private::TypeImpl > TypeImplSP
std::shared_ptr< lldb_private::Target > TargetSP
std::shared_ptr< lldb_private::File > FileSP
std::shared_ptr< lldb_private::ScriptedStackFrameRecognizerInterface > ScriptedStackFrameRecognizerInterfaceSP
std::unique_ptr< lldb_private::ScriptedProcessInterface > ScriptedProcessInterfaceUP
std::shared_ptr< lldb_private::ScriptedInstanceRegistry > ScriptedInstanceRegistrySP
std::shared_ptr< lldb_private::ScriptedFrameInterface > ScriptedFrameInterfaceSP
Describes one extension to emit into the generated template file.