9#ifndef LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SCRIPTINTERPRETERPYTHONIMPL_H
10#define LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SCRIPTINTERPRETERPYTHONIMPL_H
12#include "lldb/Host/Config.h"
24#include "llvm/ADT/STLExtras.h"
25#include "llvm/ADT/StringRef.h"
28class IOHandlerPythonInterpreter;
29class ScriptInterpreterPythonImpl :
public ScriptInterpreterPython {
31 friend class IOHandlerPythonInterpreter;
33 ScriptInterpreterPythonImpl(Debugger &debugger);
35 ~ScriptInterpreterPythonImpl()
override;
37 bool Interrupt()
override;
40 llvm::StringRef command, CommandReturnObject *result,
41 const ExecuteScriptOptions &options = ExecuteScriptOptions())
override;
43 void ExecuteInterpreterLoop()
override;
45 bool ExecuteOneLineWithReturn(
46 llvm::StringRef in_string,
47 ScriptInterpreter::ScriptReturnType return_type,
void *ret_value,
48 const ExecuteScriptOptions &options = ExecuteScriptOptions())
override;
51 const char *in_string,
52 const ExecuteScriptOptions &options = ExecuteScriptOptions())
override;
55 ExportFunctionDefinitionToInterpreter(StringList &function_def)
override;
57 bool GenerateTypeScriptFunction(StringList &input, std::string &output,
58 const void *name_token =
nullptr)
override;
60 bool GenerateTypeSynthClass(StringList &input, std::string &output,
61 const void *name_token =
nullptr)
override;
63 bool GenerateTypeSynthClass(
const char *oneliner, std::string &output,
64 const void *name_token =
nullptr)
override;
67 bool GenerateTypeScriptFunction(
const char *oneliner, std::string &output,
68 const void *name_token =
nullptr)
override;
70 bool GenerateScriptAliasFunction(StringList &input,
71 std::string &output)
override;
73 StructuredData::ObjectSP
74 CreateSyntheticScriptedProvider(
const char *class_name,
77 StructuredData::GenericSP
78 CreateScriptCommandObject(
const char *class_name)
override;
80 StructuredData::ObjectSP
81 CreateStructuredDataFromScriptObject(ScriptObject obj)
override;
83 StructuredData::GenericSP
84 CreateFrameRecognizer(
const char *class_name)
override;
87 GetRecognizedArguments(
const StructuredData::ObjectSP &implementor,
90 bool ShouldHide(
const StructuredData::ObjectSP &implementor,
98 CreateScriptedBreakpointInterface()
override;
105 CreateScriptedThreadPlanInterface()
override;
109 StructuredData::ObjectSP
110 LoadPluginModule(
const FileSpec &file_spec,
113 StructuredData::DictionarySP
114 GetDynamicSettings(StructuredData::ObjectSP plugin_module_sp, Target *target,
115 const char *setting_name,
119 uint32_t max)
override;
122 GetChildAtIndex(
const StructuredData::ObjectSP &implementor,
123 uint32_t idx)
override;
126 GetIndexOfChildWithName(
const StructuredData::ObjectSP &implementor,
127 const char *child_name)
override;
129 bool UpdateSynthProviderInstance(
130 const StructuredData::ObjectSP &implementor)
override;
132 bool MightHaveChildrenSynthProviderInstance(
133 const StructuredData::ObjectSP &implementor)
override;
136 GetSyntheticValue(
const StructuredData::ObjectSP &implementor)
override;
139 GetSyntheticTypeName(
const StructuredData::ObjectSP &implementor)
override;
142 RunScriptBasedCommand(
const char *impl_function, llvm::StringRef args,
143 ScriptedCommandSynchronicity synchronicity,
144 lldb_private::CommandReturnObject &cmd_retobj,
146 const lldb_private::ExecutionContext &exe_ctx)
override;
148 bool RunScriptBasedCommand(
149 StructuredData::GenericSP impl_obj_sp, llvm::StringRef args,
150 ScriptedCommandSynchronicity synchronicity,
151 lldb_private::CommandReturnObject &cmd_retobj, Status &
error,
152 const lldb_private::ExecutionContext &exe_ctx)
override;
154 bool RunScriptBasedParsedCommand(
155 StructuredData::GenericSP impl_obj_sp, Args &args,
156 ScriptedCommandSynchronicity synchronicity,
157 lldb_private::CommandReturnObject &cmd_retobj, Status &
error,
158 const lldb_private::ExecutionContext &exe_ctx)
override;
160 std::optional<std::string>
161 GetRepeatCommandForScriptedCommand(StructuredData::GenericSP impl_obj_sp,
162 Args &args)
override;
164 StructuredData::DictionarySP HandleArgumentCompletionForScriptedCommand(
165 StructuredData::GenericSP impl_obj_sp, std::vector<llvm::StringRef> &args,
166 size_t args_pos,
size_t char_in_arg)
override;
168 StructuredData::DictionarySP HandleOptionArgumentCompletionForScriptedCommand(
169 StructuredData::GenericSP impl_obj_sp, llvm::StringRef &long_options,
170 size_t char_in_arg)
override;
172 Status GenerateFunction(
const char *signature,
const StringList &input,
173 bool is_callback)
override;
175 Status GenerateBreakpointCommandCallbackData(StringList &input,
178 bool is_callback)
override;
180 bool GenerateWatchpointCommandCallbackData(StringList &input,
182 bool is_callback)
override;
185 StructuredData::ObjectSP &callee_wrapper_sp,
186 const TypeSummaryOptions &options,
187 std::string &retval)
override;
189 bool FormatterCallbackFunction(
const char *function_name,
192 bool GetDocumentationForItem(
const char *item, std::string &dest)
override;
194 bool GetShortHelpForCommandObject(StructuredData::GenericSP cmd_obj_sp,
195 std::string &dest)
override;
198 GetFlagsForCommandObject(StructuredData::GenericSP cmd_obj_sp)
override;
200 bool GetLongHelpForCommandObject(StructuredData::GenericSP cmd_obj_sp,
201 std::string &dest)
override;
203 StructuredData::ObjectSP
204 GetOptionsForCommandObject(StructuredData::GenericSP cmd_obj_sp)
override;
206 StructuredData::ObjectSP
207 GetArgumentsForCommandObject(StructuredData::GenericSP cmd_obj_sp)
override;
209 bool SetOptionValueForCommandObject(StructuredData::GenericSP cmd_obj_sp,
210 ExecutionContext *exe_ctx,
211 llvm::StringRef long_option,
212 llvm::StringRef value)
override;
214 void OptionParsingStartedForCommandObject(
215 StructuredData::GenericSP cmd_obj_sp)
override;
217 bool CheckObjectExists(
const char *name)
override {
218 if (!name || !name[0])
221 return GetDocumentationForItem(name, temp);
225 std::string &output, Status &
error)
override;
228 std::string &output, Status &
error)
override;
231 std::string &output, Status &
error)
override;
234 std::string &output, Status &
error)
override;
237 std::string &output, Status &
error)
override;
239 bool LoadScriptingModule(
const char *filename,
240 const LoadScriptOptions &options,
242 StructuredData::ObjectSP *module_sp =
nullptr,
243 FileSpec extra_search_dir = {},
246 bool IsReservedWord(
const char *word)
override;
248 std::unique_ptr<ScriptInterpreterLocker> AcquireInterpreterLock()
override;
250 void CollectDataForBreakpointCommandCallback(
251 std::vector<std::reference_wrapper<BreakpointOptions>> &bp_options_vec,
252 CommandReturnObject &result)
override;
255 CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options,
256 CommandReturnObject &result)
override;
259 Status SetBreakpointCommandCallback(BreakpointOptions &bp_options,
260 const char *callback_body,
261 bool is_callback)
override;
263 Status SetBreakpointCommandCallbackFunction(
264 BreakpointOptions &bp_options,
const char *function_name,
265 StructuredData::ObjectSP extra_args_sp)
override;
268 Status SetBreakpointCommandCallback(
269 BreakpointOptions &bp_options,
270 std::unique_ptr<BreakpointOptions::CommandData> &data_up)
override;
272 Status SetBreakpointCommandCallback(BreakpointOptions &bp_options,
273 const char *command_body_text,
274 StructuredData::ObjectSP extra_args_sp,
275 bool uses_extra_args,
279 void SetWatchpointCommandCallback(WatchpointOptions *wp_options,
280 const char *user_input,
281 bool is_callback)
override;
283 const char *GetDictionaryName() {
return m_dictionary_name.c_str(); }
285 PyThreadState *GetThreadState() {
return m_command_thread_state; }
287 void SetThreadState(PyThreadState *s) {
289 m_command_thread_state = s;
293 void IOHandlerActivated(IOHandler &io_handler,
bool interactive)
override;
295 void IOHandlerInputComplete(IOHandler &io_handler,
296 std::string &data)
override;
301 llvm::StringRef GetPluginName()
override {
return GetPluginNameStatic(); }
303 class Locker :
public ScriptInterpreterLocker {
306 AcquireLock = 0x0001,
307 InitSession = 0x0002,
308 InitGlobals = 0x0004,
314 FreeAcquiredLock = 0x0002,
316 TearDownSession = 0x0004
319 Locker(ScriptInterpreterPythonImpl *py_interpreter,
320 uint16_t on_entry = AcquireLock | InitSession,
321 uint16_t on_leave = FreeLock | TearDownSession,
328 bool DoAcquireLock();
330 bool DoInitSession(uint16_t on_entry_flags,
lldb::FileSP in,
335 bool DoTearDownSession();
337 bool m_teardown_session;
338 ScriptInterpreterPythonImpl *m_python_interpreter;
339 PyGILState_STATE m_GILState;
342 static bool BreakpointCallbackFunction(
void *baton,
343 StoppointCallbackContext *context,
346 static bool WatchpointCallbackFunction(
void *baton,
347 StoppointCallbackContext *context,
349 static void Initialize();
351 class SynchronicityHandler {
360 ~SynchronicityHandler();
363 enum class AddLocation { Beginning, End };
365 static void AddToSysPath(AddLocation location, std::string path);
372 uint32_t IsExecutingPython() {
373 std::lock_guard<std::mutex> guard(m_mutex);
374 return m_lock_count > 0;
377 uint32_t IncrementLockCount() {
378 std::lock_guard<std::mutex> guard(m_mutex);
379 return ++m_lock_count;
382 uint32_t DecrementLockCount() {
383 std::lock_guard<std::mutex> guard(m_mutex);
384 if (m_lock_count > 0)
395 python::PythonModule &GetMainModule();
397 python::PythonDictionary &GetSessionDictionary();
399 python::PythonDictionary &GetSysModuleDictionary();
401 llvm::Expected<unsigned> GetMaxPositionalArgumentsForCallable(
402 const llvm::StringRef &callable_name)
override;
404 bool GetEmbeddedInterpreterModuleObjects();
406 bool SetStdHandle(
lldb::FileSP file,
const char *py_name,
407 python::PythonObject &save_file,
const char *mode);
409 python::PythonObject m_saved_stdin;
410 python::PythonObject m_saved_stdout;
411 python::PythonObject m_saved_stderr;
412 python::PythonModule m_main_module;
413 python::PythonDictionary m_session_dict;
414 python::PythonDictionary m_sys_module_dict;
415 python::PythonObject m_run_one_line_function;
416 python::PythonObject m_run_one_line_str_global;
417 std::string m_dictionary_name;
419 bool m_session_is_active;
420 bool m_pty_secondary_is_open;
421 bool m_valid_session;
422 uint32_t m_lock_count;
424 PyThreadState *m_command_thread_state;
427class IOHandlerPythonInterpreter :
public IOHandler {
429 IOHandlerPythonInterpreter(Debugger &debugger,
430 ScriptInterpreterPythonImpl *python)
431 : IOHandler(debugger, IOHandler::
Type::PythonInterpreter),
434 ~IOHandlerPythonInterpreter()
override =
default;
436 llvm::StringRef GetControlSequence(
char ch)
override {
437 static constexpr llvm::StringLiteral control_sequence(
"quit()\n");
439 return control_sequence;
443 void Run()
override {
445 int stdin_fd = GetInputFD();
447 Terminal terminal(stdin_fd);
448 TerminalState terminal_state(terminal);
450 if (terminal.IsATerminal()) {
452 llvm::consumeError(terminal.SetCanonical(
false));
453 llvm::consumeError(terminal.SetEcho(
true));
456 ScriptInterpreterPythonImpl::Locker locker(
458 ScriptInterpreterPythonImpl::Locker::AcquireLock |
459 ScriptInterpreterPythonImpl::Locker::InitSession |
460 ScriptInterpreterPythonImpl::Locker::InitGlobals,
461 ScriptInterpreterPythonImpl::Locker::FreeAcquiredLock |
462 ScriptInterpreterPythonImpl::Locker::TearDownSession);
477 StreamString run_string;
478 run_string.Printf(
"run_python_interpreter (%s)",
479 m_python->GetDictionaryName());
480 python::RunSimpleString(run_string.GetData());
486 void Cancel()
override {}
488 bool Interrupt()
override {
return m_python->Interrupt(); }
490 void GotEOF()
override {}
493 ScriptInterpreterPythonImpl *m_python;
static llvm::raw_ostream & error(Stream &strm)
static std::optional< size_t > CalculateNumChildren(CompilerType container_elem_type, uint64_t num_elements, CompilerType element_type)
Calculates the number of elements stored in a container (with element type 'container_elem_type') as ...
A class that represents a running process on the host machine.
ScriptedCommandSynchronicity
std::shared_ptr< lldb_private::ScriptedStopHookInterface > ScriptedStopHookInterfaceSP
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
std::shared_ptr< lldb_private::ScriptInterpreter > ScriptInterpreterSP
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::ValueObjectList > ValueObjectListSP
std::shared_ptr< lldb_private::Debugger > DebuggerSP
std::shared_ptr< lldb_private::TypeImpl > TypeImplSP
std::shared_ptr< lldb_private::Target > TargetSP
std::shared_ptr< lldb_private::File > FileSP
std::unique_ptr< lldb_private::ScriptedProcessInterface > ScriptedProcessInterfaceUP
std::shared_ptr< lldb_private::ScriptedFrameInterface > ScriptedFrameInterfaceSP