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
26#include "lldb/Utility/Status.h"
29#include "lldb/lldb-private.h"
30#include <optional>
31
32namespace lldb_private {
33
35public:
37
38 virtual ~ScriptInterpreterLocker() = default;
39
40private:
44};
45
47public:
49
50 bool GetEnableIO() const { return m_enable_io; }
51
52 bool GetSetLLDBGlobals() const { return m_set_lldb_globals; }
53
54 // If this is true then any exceptions raised by the script will be
55 // cleared with PyErr_Clear(). If false then they will be left for
56 // the caller to clean up
57 bool GetMaskoutErrors() const { return m_maskout_errors; }
58
60 m_enable_io = enable;
61 return *this;
62 }
63
66 return *this;
67 }
68
70 m_maskout_errors = maskout;
71 return *this;
72 }
73
74private:
75 bool m_enable_io = true;
76 bool m_set_lldb_globals = true;
77 bool m_maskout_errors = true;
78};
79
81public:
82 LoadScriptOptions() = default;
83
84 bool GetInitSession() const { return m_init_session; }
85 bool GetSilent() const { return m_silent; }
86
89 return *this;
90 }
91
93 m_silent = b;
94 return *this;
95 }
96
97private:
98 bool m_init_session = false;
99 bool m_silent = false;
100};
101
103public:
104 /// Create an IO redirect. If IO is enabled, this will redirects the output
105 /// to the command return object if set or to the debugger otherwise. If IO
106 /// is disabled, it will redirect all IO to /dev/null.
107 static llvm::Expected<std::unique_ptr<ScriptInterpreterIORedirect>>
108 Create(bool enable_io, Debugger &debugger, CommandReturnObject *result);
109
111
114 return m_output_file_sp->GetUnlockedFileSP();
115 }
117 return m_error_file_sp->GetUnlockedFileSP();
118 }
119
120 /// Flush our output and error file handles.
121 void Flush();
122
123private:
124 ScriptInterpreterIORedirect(std::unique_ptr<File> input,
125 std::unique_ptr<File> output);
127
134};
135
137public:
155
156 ScriptInterpreter(Debugger &debugger, lldb::ScriptLanguage script_lang);
157
159
160 /// Describes one extension to emit into the generated template file.
161 ///
162 /// `full_name` is the dotted extension name as the user typed it (e.g.
163 /// `"ScriptedProcess"` or a nested `"...ChildKind"`), used to name the
164 /// generated class. `path` is the same name split on `.`; its last element
165 /// selects the ScriptedExtension kind and everything before it is treated
166 /// as a subclass discriminator. Both StringRefs must outlive the call to
167 /// GenerateExtensionTemplate; the implementation stores them, not copies.
169 llvm::StringRef full_name;
170 llvm::SmallVector<llvm::StringRef> path;
171 };
172
173 virtual llvm::Expected<FileSpec>
174 GenerateExtensionTemplate(const std::string &name,
175 std::vector<ExtensionTemplateRequest> &extensions,
176 bool generate_non_abstract_methods,
177 std::string output_file);
178
179 ~ScriptInterpreter() override = default;
180
181 virtual bool Interrupt() { return false; }
182
183 virtual bool ExecuteOneLine(
184 llvm::StringRef command, CommandReturnObject *result,
185 const ExecuteScriptOptions &options = ExecuteScriptOptions()) = 0;
186
187 virtual void ExecuteInterpreterLoop() = 0;
188
190 llvm::StringRef in_string, ScriptReturnType return_type, void *ret_value,
191 const ExecuteScriptOptions &options = ExecuteScriptOptions()) {
192 return true;
193 }
194
196 const char *in_string,
197 const ExecuteScriptOptions &options = ExecuteScriptOptions()) {
198 return Status::FromErrorString("not implemented");
199 }
200
201 virtual Status
203 return Status::FromErrorString("not implemented");
204 }
205
207 std::string &output,
208 bool has_extra_args,
209 bool is_callback) {
210 return Status::FromErrorString("not implemented");
211 }
212
214 std::string &output,
215 bool is_callback) {
216 return false;
217 }
218
219 virtual bool GenerateTypeScriptFunction(const char *oneliner,
220 std::string &output,
221 const void *name_token = nullptr) {
222 return false;
223 }
224
226 std::string &output,
227 const void *name_token = nullptr) {
228 return false;
229 }
230
232 std::string &output) {
233 return false;
234 }
235
236 virtual bool GenerateTypeSynthClass(StringList &input, std::string &output,
237 const void *name_token = nullptr) {
238 return false;
239 }
240
241 virtual bool GenerateTypeSynthClass(const char *oneliner, std::string &output,
242 const void *name_token = nullptr) {
243 return false;
244 }
245
250
253 const char *setting_name, lldb_private::Status &error) {
255 }
256
257 virtual Status GenerateFunction(const char *signature,
258 const StringList &input,
259 bool is_callback) {
260 return Status::FromErrorString("not implemented");
261 }
262
264 std::vector<std::reference_wrapper<BreakpointOptions>> &options,
265 CommandReturnObject &result);
266
267 virtual void
269 CommandReturnObject &result);
270
271 /// Set the specified text as the callback for the breakpoint.
273 std::vector<std::reference_wrapper<BreakpointOptions>> &bp_options_vec,
274 const char *callback_text);
275
277 const char *callback_text,
278 bool is_callback) {
279 return Status::FromErrorString("not implemented");
280 }
281
282 /// This one is for deserialization:
284 BreakpointOptions &bp_options,
285 std::unique_ptr<BreakpointOptions::CommandData> &data_up) {
286 return Status::FromErrorString("not implemented");
287 }
288
290 std::vector<std::reference_wrapper<BreakpointOptions>> &bp_options_vec,
291 const char *function_name, StructuredData::ObjectSP extra_args_sp);
292
293 /// Set a script function as the callback for the breakpoint.
294 virtual Status
296 const char *function_name,
297 StructuredData::ObjectSP extra_args_sp) {
298 return Status::FromErrorString("not implemented");
299 }
300
301 /// Set a one-liner as the callback for the watchpoint.
303 const char *user_input,
304 bool is_callback) {}
305
306 virtual bool GetScriptedSummary(const char *function_name,
307 lldb::ValueObjectSP valobj,
308 StructuredData::ObjectSP &callee_wrapper_sp,
309 const TypeSummaryOptions &options,
310 std::string &retval) {
311 return false;
312 }
313
314 // Calls the specified formatter matching Python function and returns its
315 // result (true if it's a match, false if we should keep looking for a
316 // matching formatter).
317 virtual bool FormatterCallbackFunction(const char *function_name,
318 lldb::TypeImplSP type_impl_sp) {
319 return true;
320 }
321
322 virtual void Clear() {
323 // Clean up any ref counts to SBObjects that might be in global variables
324 }
325
326 virtual bool
327 RunScriptBasedCommand(const char *impl_function, llvm::StringRef args,
328 ScriptedCommandSynchronicity synchronicity,
330 Status &error,
331 const lldb_private::ExecutionContext &exe_ctx) {
332 return false;
333 }
334
335 virtual bool RunScriptFormatKeyword(const char *impl_function,
336 Process *process, std::string &output,
337 Status &error) {
338 error = Status::FromErrorString("unimplemented");
339 return false;
340 }
341
342 virtual bool RunScriptFormatKeyword(const char *impl_function, Thread *thread,
343 std::string &output, Status &error) {
344 error = Status::FromErrorString("unimplemented");
345 return false;
346 }
347
348 virtual bool RunScriptFormatKeyword(const char *impl_function, Target *target,
349 std::string &output, Status &error) {
350 error = Status::FromErrorString("unimplemented");
351 return false;
352 }
353
354 virtual bool RunScriptFormatKeyword(const char *impl_function,
355 StackFrame *frame, std::string &output,
356 Status &error) {
357 error = Status::FromErrorString("unimplemented");
358 return false;
359 }
360
361 virtual bool RunScriptFormatKeyword(const char *impl_function,
362 ValueObject *value, std::string &output,
363 Status &error) {
364 error = Status::FromErrorString("unimplemented");
365 return false;
366 }
367
368 virtual bool GetDocumentationForItem(const char *item, std::string &dest) {
369 dest.clear();
370 return false;
371 }
372
373 virtual bool CheckObjectExists(const char *name) { return false; }
374
375 virtual bool
376 LoadScriptingModule(const char *filename, const LoadScriptOptions &options,
378 StructuredData::ObjectSP *module_sp = nullptr,
379 FileSpec extra_search_dir = {},
380 lldb::TargetSP loaded_into_target_sp = {});
381
382 virtual bool IsReservedWord(const char *word) { return false; }
383
384 virtual std::unique_ptr<ScriptInterpreterLocker> AcquireInterpreterLock();
385
387
388 virtual llvm::Expected<unsigned>
389 GetMaxPositionalArgumentsForCallable(const llvm::StringRef &callable_name) {
390 return llvm::createStringError(
391 llvm::inconvertibleErrorCode(), "Unimplemented function");
392 }
393
394 static std::string LanguageToString(lldb::ScriptLanguage language);
395
396 static lldb::ScriptLanguage StringToLanguage(const llvm::StringRef &string);
397
398 virtual llvm::Expected<std::string>
400 return llvm::make_error<UnimplementedError>();
401 }
402
403 static llvm::StringLiteral
405
406 static lldb::ScriptedExtension StringToExtension(llvm::StringRef string);
407
409
413
417
421
424 return {};
425 }
426
429 return {};
430 }
431
435
439
443
446 return {};
447 }
448
453
457
460 return {};
461 }
462
467
472
473 /// Holds an lldb_private::Module name and a "sanitized" version
474 /// of it for the purposes of loading a script of that name by
475 /// the relevant ScriptInterpreter.
476 ///
477 /// E.g., for Python the sanitized name can't include:
478 /// * Special characters: '-', ' ', '.'
479 /// * Python keywords
481 public:
482 SanitizedScriptingModuleName(std::string name, std::string sanitized_name,
483 std::string conflicting_keyword)
484 : m_original_name(std::move(name)),
485 m_sanitized_name(std::move(sanitized_name)),
486 m_conflicting_keyword(std::move(conflicting_keyword)) {}
487
488 /// Returns \c true if this name is a keyword in the associated scripting
489 /// language.
490 bool IsKeyword() const { return !m_conflicting_keyword.empty(); }
491
492 /// Returns \c true if the original name has been sanitized (i.e., required
493 /// changes).
494 bool RequiredSanitization() const {
496 }
497
498 llvm::StringRef GetSanitizedName() const { return m_sanitized_name; }
499 llvm::StringRef GetOriginalName() const { return m_original_name; }
500 llvm::StringRef GetConflictingKeyword() const {
502 }
503
504 private:
505 std::string m_original_name;
506 std::string m_sanitized_name;
507
508 /// If the m_sanitized_name conflicts with a keyword for the
509 /// ScriptInterpreter language associated with this
510 /// SanitizedScriptingModuleName, is set to the conflicting keyword. Empty
511 /// otherwise.
513 };
514
516 GetSanitizedScriptingModuleName(llvm::StringRef name);
517
518 /// Get the debugger associated with this script interpreter.
520 const Debugger &GetDebugger() const { return m_debugger; }
521
522protected:
525};
526
527} // namespace lldb_private
528
529#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:100
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:359
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)
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)
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::ScriptedFrameInterface > ScriptedFrameInterfaceSP
Describes one extension to emit into the generated template file.