1 //===-- ScriptInterpreter.h -------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef liblldb_ScriptInterpreter_h_
10 #define liblldb_ScriptInterpreter_h_
12 #include "lldb/lldb-private.h"
14 #include "lldb/Breakpoint/BreakpointOptions.h"
15 #include "lldb/Core/PluginInterface.h"
16 #include "lldb/Core/SearchFilter.h"
17 #include "lldb/Utility/Broadcaster.h"
18 #include "lldb/Utility/Status.h"
19 #include "lldb/Utility/StructuredData.h"
21 #include "lldb/Host/PseudoTerminal.h"
23 namespace lldb_private {
25 class ScriptInterpreterLocker {
27 ScriptInterpreterLocker() = default;
29 virtual ~ScriptInterpreterLocker() = default;
32 DISALLOW_COPY_AND_ASSIGN(ScriptInterpreterLocker);
35 class ScriptInterpreter : public PluginInterface {
37 enum ScriptReturnType {
38 eScriptReturnTypeCharPtr,
39 eScriptReturnTypeBool,
40 eScriptReturnTypeShortInt,
41 eScriptReturnTypeShortIntUnsigned,
43 eScriptReturnTypeIntUnsigned,
44 eScriptReturnTypeLongInt,
45 eScriptReturnTypeLongIntUnsigned,
46 eScriptReturnTypeLongLong,
47 eScriptReturnTypeLongLongUnsigned,
48 eScriptReturnTypeFloat,
49 eScriptReturnTypeDouble,
50 eScriptReturnTypeChar,
51 eScriptReturnTypeCharStrOrNone,
52 eScriptReturnTypeOpaqueObject
55 ScriptInterpreter(Debugger &debugger, lldb::ScriptLanguage script_lang);
57 ~ScriptInterpreter() override;
59 struct ExecuteScriptOptions {
61 ExecuteScriptOptions()
62 : m_enable_io(true), m_set_lldb_globals(true), m_maskout_errors(true) {}
64 bool GetEnableIO() const { return m_enable_io; }
66 bool GetSetLLDBGlobals() const { return m_set_lldb_globals; }
68 bool GetMaskoutErrors() const { return m_maskout_errors; }
70 ExecuteScriptOptions &SetEnableIO(bool enable) {
75 ExecuteScriptOptions &SetSetLLDBGlobals(bool set) {
76 m_set_lldb_globals = set;
80 ExecuteScriptOptions &SetMaskoutErrors(bool maskout) {
81 m_maskout_errors = maskout;
87 bool m_set_lldb_globals;
88 bool m_maskout_errors;
91 virtual bool Interrupt() { return false; }
93 virtual bool ExecuteOneLine(
94 llvm::StringRef command, CommandReturnObject *result,
95 const ExecuteScriptOptions &options = ExecuteScriptOptions()) = 0;
97 virtual void ExecuteInterpreterLoop() = 0;
99 virtual bool ExecuteOneLineWithReturn(
100 llvm::StringRef in_string, ScriptReturnType return_type, void *ret_value,
101 const ExecuteScriptOptions &options = ExecuteScriptOptions()) {
105 virtual Status ExecuteMultipleLines(
106 const char *in_string,
107 const ExecuteScriptOptions &options = ExecuteScriptOptions()) {
109 error.SetErrorString("not implemented");
114 ExportFunctionDefinitionToInterpreter(StringList &function_def) {
116 error.SetErrorString("not implemented");
120 virtual Status GenerateBreakpointCommandCallbackData(StringList &input,
121 std::string &output) {
123 error.SetErrorString("not implemented");
127 virtual bool GenerateWatchpointCommandCallbackData(StringList &input,
128 std::string &output) {
132 virtual bool GenerateTypeScriptFunction(const char *oneliner,
134 const void *name_token = nullptr) {
138 virtual bool GenerateTypeScriptFunction(StringList &input,
140 const void *name_token = nullptr) {
144 virtual bool GenerateScriptAliasFunction(StringList &input,
145 std::string &output) {
149 virtual bool GenerateTypeSynthClass(StringList &input, std::string &output,
150 const void *name_token = nullptr) {
154 virtual bool GenerateTypeSynthClass(const char *oneliner, std::string &output,
155 const void *name_token = nullptr) {
159 virtual StructuredData::ObjectSP
160 CreateSyntheticScriptedProvider(const char *class_name,
161 lldb::ValueObjectSP valobj) {
162 return StructuredData::ObjectSP();
165 virtual StructuredData::GenericSP
166 CreateScriptCommandObject(const char *class_name) {
167 return StructuredData::GenericSP();
170 virtual StructuredData::GenericSP
171 CreateFrameRecognizer(const char *class_name) {
172 return StructuredData::GenericSP();
175 virtual lldb::ValueObjectListSP GetRecognizedArguments(
176 const StructuredData::ObjectSP &implementor,
177 lldb::StackFrameSP frame_sp) {
178 return lldb::ValueObjectListSP();
181 virtual StructuredData::GenericSP
182 OSPlugin_CreatePluginObject(const char *class_name,
183 lldb::ProcessSP process_sp) {
184 return StructuredData::GenericSP();
187 virtual StructuredData::DictionarySP
188 OSPlugin_RegisterInfo(StructuredData::ObjectSP os_plugin_object_sp) {
189 return StructuredData::DictionarySP();
192 virtual StructuredData::ArraySP
193 OSPlugin_ThreadsInfo(StructuredData::ObjectSP os_plugin_object_sp) {
194 return StructuredData::ArraySP();
197 virtual StructuredData::StringSP
198 OSPlugin_RegisterContextData(StructuredData::ObjectSP os_plugin_object_sp,
199 lldb::tid_t thread_id) {
200 return StructuredData::StringSP();
203 virtual StructuredData::DictionarySP
204 OSPlugin_CreateThread(StructuredData::ObjectSP os_plugin_object_sp,
205 lldb::tid_t tid, lldb::addr_t context) {
206 return StructuredData::DictionarySP();
209 virtual StructuredData::ObjectSP
210 CreateScriptedThreadPlan(const char *class_name,
211 lldb::ThreadPlanSP thread_plan_sp) {
212 return StructuredData::ObjectSP();
216 ScriptedThreadPlanExplainsStop(StructuredData::ObjectSP implementor_sp,
217 Event *event, bool &script_error) {
223 ScriptedThreadPlanShouldStop(StructuredData::ObjectSP implementor_sp,
224 Event *event, bool &script_error) {
230 ScriptedThreadPlanIsStale(StructuredData::ObjectSP implementor_sp,
231 bool &script_error) {
236 virtual lldb::StateType
237 ScriptedThreadPlanGetRunState(StructuredData::ObjectSP implementor_sp,
238 bool &script_error) {
240 return lldb::eStateStepping;
243 virtual StructuredData::GenericSP
244 CreateScriptedBreakpointResolver(const char *class_name,
245 StructuredDataImpl *args_data,
246 lldb::BreakpointSP &bkpt_sp) {
247 return StructuredData::GenericSP();
251 ScriptedBreakpointResolverSearchCallback(StructuredData::GenericSP implementor_sp,
252 SymbolContext *sym_ctx)
257 virtual lldb::SearchDepth
258 ScriptedBreakpointResolverSearchDepth(StructuredData::GenericSP implementor_sp)
260 return lldb::eSearchDepthModule;
263 virtual StructuredData::ObjectSP
264 LoadPluginModule(const FileSpec &file_spec, lldb_private::Status &error) {
265 return StructuredData::ObjectSP();
268 virtual StructuredData::DictionarySP
269 GetDynamicSettings(StructuredData::ObjectSP plugin_module_sp, Target *target,
270 const char *setting_name, lldb_private::Status &error) {
271 return StructuredData::DictionarySP();
274 virtual Status GenerateFunction(const char *signature,
275 const StringList &input) {
277 error.SetErrorString("unimplemented");
281 virtual void CollectDataForBreakpointCommandCallback(
282 std::vector<BreakpointOptions *> &options, CommandReturnObject &result);
285 CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options,
286 CommandReturnObject &result);
288 /// Set the specified text as the callback for the breakpoint.
290 SetBreakpointCommandCallback(std::vector<BreakpointOptions *> &bp_options_vec,
291 const char *callback_text);
293 virtual Status SetBreakpointCommandCallback(BreakpointOptions *bp_options,
294 const char *callback_text) {
296 error.SetErrorString("unimplemented");
300 /// This one is for deserialization:
301 virtual Status SetBreakpointCommandCallback(
302 BreakpointOptions *bp_options,
303 std::unique_ptr<BreakpointOptions::CommandData> &data_up) {
305 error.SetErrorString("unimplemented");
309 void SetBreakpointCommandCallbackFunction(
310 std::vector<BreakpointOptions *> &bp_options_vec,
311 const char *function_name);
313 /// Set a one-liner as the callback for the breakpoint.
315 SetBreakpointCommandCallbackFunction(BreakpointOptions *bp_options,
316 const char *function_name) {}
318 /// Set a one-liner as the callback for the watchpoint.
319 virtual void SetWatchpointCommandCallback(WatchpointOptions *wp_options,
320 const char *oneliner) {}
322 virtual bool GetScriptedSummary(const char *function_name,
323 lldb::ValueObjectSP valobj,
324 StructuredData::ObjectSP &callee_wrapper_sp,
325 const TypeSummaryOptions &options,
326 std::string &retval) {
330 virtual void Clear() {
331 // Clean up any ref counts to SBObjects that might be in global variables
335 CalculateNumChildren(const StructuredData::ObjectSP &implementor,
340 virtual lldb::ValueObjectSP
341 GetChildAtIndex(const StructuredData::ObjectSP &implementor, uint32_t idx) {
342 return lldb::ValueObjectSP();
346 GetIndexOfChildWithName(const StructuredData::ObjectSP &implementor,
347 const char *child_name) {
352 UpdateSynthProviderInstance(const StructuredData::ObjectSP &implementor) {
356 virtual bool MightHaveChildrenSynthProviderInstance(
357 const StructuredData::ObjectSP &implementor) {
361 virtual lldb::ValueObjectSP
362 GetSyntheticValue(const StructuredData::ObjectSP &implementor) {
367 GetSyntheticTypeName(const StructuredData::ObjectSP &implementor) {
368 return ConstString();
372 RunScriptBasedCommand(const char *impl_function, llvm::StringRef args,
373 ScriptedCommandSynchronicity synchronicity,
374 lldb_private::CommandReturnObject &cmd_retobj,
376 const lldb_private::ExecutionContext &exe_ctx) {
380 virtual bool RunScriptBasedCommand(
381 StructuredData::GenericSP impl_obj_sp, llvm::StringRef args,
382 ScriptedCommandSynchronicity synchronicity,
383 lldb_private::CommandReturnObject &cmd_retobj, Status &error,
384 const lldb_private::ExecutionContext &exe_ctx) {
388 virtual bool RunScriptFormatKeyword(const char *impl_function,
389 Process *process, std::string &output,
391 error.SetErrorString("unimplemented");
395 virtual bool RunScriptFormatKeyword(const char *impl_function, Thread *thread,
396 std::string &output, Status &error) {
397 error.SetErrorString("unimplemented");
401 virtual bool RunScriptFormatKeyword(const char *impl_function, Target *target,
402 std::string &output, Status &error) {
403 error.SetErrorString("unimplemented");
407 virtual bool RunScriptFormatKeyword(const char *impl_function,
408 StackFrame *frame, std::string &output,
410 error.SetErrorString("unimplemented");
414 virtual bool RunScriptFormatKeyword(const char *impl_function,
415 ValueObject *value, std::string &output,
417 error.SetErrorString("unimplemented");
421 virtual bool GetDocumentationForItem(const char *item, std::string &dest) {
427 GetShortHelpForCommandObject(StructuredData::GenericSP cmd_obj_sp,
434 GetFlagsForCommandObject(StructuredData::GenericSP cmd_obj_sp) {
438 virtual bool GetLongHelpForCommandObject(StructuredData::GenericSP cmd_obj_sp,
444 virtual bool CheckObjectExists(const char *name) { return false; }
447 LoadScriptingModule(const char *filename, bool can_reload, bool init_session,
448 lldb_private::Status &error,
449 StructuredData::ObjectSP *module_sp = nullptr) {
450 error.SetErrorString("loading unimplemented");
454 virtual bool IsReservedWord(const char *word) { return false; }
456 virtual std::unique_ptr<ScriptInterpreterLocker> AcquireInterpreterLock();
458 const char *GetScriptInterpreterPtyName();
460 int GetMasterFileDescriptor();
462 static std::string LanguageToString(lldb::ScriptLanguage language);
464 static lldb::ScriptLanguage StringToLanguage(const llvm::StringRef &string);
466 virtual void ResetOutputFileHandle(FILE *new_fh) {} // By default, do nothing.
468 lldb::ScriptLanguage GetLanguage() { return m_script_lang; }
471 Debugger &m_debugger;
472 lldb::ScriptLanguage m_script_lang;
475 } // namespace lldb_private
477 #endif // liblldb_ScriptInterpreter_h_