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 CreateScriptedBreakpointResolver(
const char *class_name,
85 const StructuredDataImpl &args_data,
87 bool ScriptedBreakpointResolverSearchCallback(
88 StructuredData::GenericSP implementor_sp,
89 SymbolContext *sym_ctx)
override;
92 StructuredData::GenericSP implementor_sp)
override;
94 StructuredData::GenericSP
95 CreateScriptedStopHook(
lldb::TargetSP target_sp,
const char *class_name,
96 const StructuredDataImpl &args_data,
97 Status &
error)
override;
99 bool ScriptedStopHookHandleStop(StructuredData::GenericSP implementor_sp,
100 ExecutionContext &exc_ctx,
103 StructuredData::GenericSP
104 CreateFrameRecognizer(
const char *class_name)
override;
107 GetRecognizedArguments(
const StructuredData::ObjectSP &implementor,
110 bool ShouldHide(
const StructuredData::ObjectSP &implementor,
118 CreateScriptedThreadPlanInterface()
override;
122 StructuredData::ObjectSP
123 LoadPluginModule(
const FileSpec &file_spec,
126 StructuredData::DictionarySP
127 GetDynamicSettings(StructuredData::ObjectSP plugin_module_sp, Target *target,
128 const char *setting_name,
132 uint32_t max)
override;
135 GetChildAtIndex(
const StructuredData::ObjectSP &implementor,
136 uint32_t idx)
override;
138 int GetIndexOfChildWithName(
const StructuredData::ObjectSP &implementor,
139 const char *child_name)
override;
141 bool UpdateSynthProviderInstance(
142 const StructuredData::ObjectSP &implementor)
override;
144 bool MightHaveChildrenSynthProviderInstance(
145 const StructuredData::ObjectSP &implementor)
override;
148 GetSyntheticValue(
const StructuredData::ObjectSP &implementor)
override;
151 GetSyntheticTypeName(
const StructuredData::ObjectSP &implementor)
override;
154 RunScriptBasedCommand(
const char *impl_function, llvm::StringRef args,
155 ScriptedCommandSynchronicity synchronicity,
160 bool RunScriptBasedCommand(
161 StructuredData::GenericSP impl_obj_sp, llvm::StringRef args,
162 ScriptedCommandSynchronicity synchronicity,
166 bool RunScriptBasedParsedCommand(
167 StructuredData::GenericSP impl_obj_sp, Args &args,
168 ScriptedCommandSynchronicity synchronicity,
172 std::optional<std::string>
173 GetRepeatCommandForScriptedCommand(StructuredData::GenericSP impl_obj_sp,
174 Args &args)
override;
176 Status GenerateFunction(
const char *signature,
const StringList &input,
177 bool is_callback)
override;
179 Status GenerateBreakpointCommandCallbackData(StringList &input,
182 bool is_callback)
override;
184 bool GenerateWatchpointCommandCallbackData(StringList &input,
186 bool is_callback)
override;
189 StructuredData::ObjectSP &callee_wrapper_sp,
190 const TypeSummaryOptions &options,
191 std::string &retval)
override;
193 bool FormatterCallbackFunction(
const char *function_name,
196 bool GetDocumentationForItem(
const char *item, std::string &dest)
override;
198 bool GetShortHelpForCommandObject(StructuredData::GenericSP cmd_obj_sp,
199 std::string &dest)
override;
202 GetFlagsForCommandObject(StructuredData::GenericSP cmd_obj_sp)
override;
204 bool GetLongHelpForCommandObject(StructuredData::GenericSP cmd_obj_sp,
205 std::string &dest)
override;
207 StructuredData::ObjectSP
208 GetOptionsForCommandObject(StructuredData::GenericSP cmd_obj_sp)
override;
210 StructuredData::ObjectSP
211 GetArgumentsForCommandObject(StructuredData::GenericSP cmd_obj_sp)
override;
213 bool SetOptionValueForCommandObject(StructuredData::GenericSP cmd_obj_sp,
214 ExecutionContext *exe_ctx,
215 llvm::StringRef long_option,
216 llvm::StringRef value)
override;
218 void OptionParsingStartedForCommandObject(
219 StructuredData::GenericSP cmd_obj_sp)
override;
221 bool CheckObjectExists(
const char *name)
override {
222 if (!name || !name[0])
225 return GetDocumentationForItem(name, temp);
229 std::string &output, Status &
error)
override;
232 std::string &output, Status &
error)
override;
235 std::string &output, Status &
error)
override;
238 std::string &output, Status &
error)
override;
241 std::string &output, Status &
error)
override;
243 bool LoadScriptingModule(
const char *filename,
244 const LoadScriptOptions &options,
246 StructuredData::ObjectSP *module_sp =
nullptr,
247 FileSpec extra_search_dir = {})
override;
249 bool IsReservedWord(
const char *word)
override;
251 std::unique_ptr<ScriptInterpreterLocker> AcquireInterpreterLock()
override;
253 void CollectDataForBreakpointCommandCallback(
254 std::vector<std::reference_wrapper<BreakpointOptions>> &bp_options_vec,
255 CommandReturnObject &result)
override;
258 CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options,
259 CommandReturnObject &result)
override;
262 Status SetBreakpointCommandCallback(BreakpointOptions &bp_options,
263 const char *callback_body,
264 bool is_callback)
override;
266 Status SetBreakpointCommandCallbackFunction(
267 BreakpointOptions &bp_options,
const char *function_name,
268 StructuredData::ObjectSP extra_args_sp)
override;
271 Status SetBreakpointCommandCallback(
272 BreakpointOptions &bp_options,
273 std::unique_ptr<BreakpointOptions::CommandData> &data_up)
override;
275 Status SetBreakpointCommandCallback(BreakpointOptions &bp_options,
276 const char *command_body_text,
277 StructuredData::ObjectSP extra_args_sp,
278 bool uses_extra_args,
282 void SetWatchpointCommandCallback(WatchpointOptions *wp_options,
283 const char *user_input,
284 bool is_callback)
override;
286 const char *GetDictionaryName() {
return m_dictionary_name.c_str(); }
288 PyThreadState *GetThreadState() {
return m_command_thread_state; }
290 void SetThreadState(PyThreadState *s) {
292 m_command_thread_state = s;
296 void IOHandlerActivated(IOHandler &io_handler,
bool interactive)
override;
298 void IOHandlerInputComplete(IOHandler &io_handler,
299 std::string &data)
override;
304 llvm::StringRef GetPluginName()
override {
return GetPluginNameStatic(); }
306 class Locker :
public ScriptInterpreterLocker {
309 AcquireLock = 0x0001,
310 InitSession = 0x0002,
311 InitGlobals = 0x0004,
317 FreeAcquiredLock = 0x0002,
319 TearDownSession = 0x0004
322 Locker(ScriptInterpreterPythonImpl *py_interpreter,
323 uint16_t on_entry = AcquireLock | InitSession,
324 uint16_t on_leave = FreeLock | TearDownSession,
331 bool DoAcquireLock();
333 bool DoInitSession(uint16_t on_entry_flags,
lldb::FileSP in,
338 bool DoTearDownSession();
340 bool m_teardown_session;
341 ScriptInterpreterPythonImpl *m_python_interpreter;
342 PyGILState_STATE m_GILState;
345 static bool BreakpointCallbackFunction(
void *baton,
346 StoppointCallbackContext *context,
349 static bool WatchpointCallbackFunction(
void *baton,
350 StoppointCallbackContext *context,
352 static void Initialize();
354 class SynchronicityHandler {
363 ~SynchronicityHandler();
366 enum class AddLocation { Beginning, End };
368 static void AddToSysPath(AddLocation location, std::string path);
375 uint32_t IsExecutingPython() {
376 std::lock_guard<std::mutex> guard(m_mutex);
377 return m_lock_count > 0;
380 uint32_t IncrementLockCount() {
381 std::lock_guard<std::mutex> guard(m_mutex);
382 return ++m_lock_count;
385 uint32_t DecrementLockCount() {
386 std::lock_guard<std::mutex> guard(m_mutex);
387 if (m_lock_count > 0)
398 python::PythonModule &GetMainModule();
400 python::PythonDictionary &GetSessionDictionary();
402 python::PythonDictionary &GetSysModuleDictionary();
404 llvm::Expected<unsigned> GetMaxPositionalArgumentsForCallable(
405 const llvm::StringRef &callable_name)
override;
407 bool GetEmbeddedInterpreterModuleObjects();
409 bool SetStdHandle(
lldb::FileSP file,
const char *py_name,
410 python::PythonObject &save_file,
const char *mode);
412 python::PythonObject m_saved_stdin;
413 python::PythonObject m_saved_stdout;
414 python::PythonObject m_saved_stderr;
415 python::PythonModule m_main_module;
416 python::PythonDictionary m_session_dict;
417 python::PythonDictionary m_sys_module_dict;
418 python::PythonObject m_run_one_line_function;
419 python::PythonObject m_run_one_line_str_global;
420 std::string m_dictionary_name;
422 bool m_session_is_active;
423 bool m_pty_secondary_is_open;
424 bool m_valid_session;
425 uint32_t m_lock_count;
427 PyThreadState *m_command_thread_state;
430class IOHandlerPythonInterpreter :
public IOHandler {
432 IOHandlerPythonInterpreter(Debugger &debugger,
433 ScriptInterpreterPythonImpl *python)
434 : IOHandler(debugger, IOHandler::
Type::PythonInterpreter),
437 ~IOHandlerPythonInterpreter()
override =
default;
439 llvm::StringRef GetControlSequence(
char ch)
override {
440 static constexpr llvm::StringLiteral control_sequence(
"quit()\n");
442 return control_sequence;
446 void Run()
override {
448 int stdin_fd = GetInputFD();
450 Terminal terminal(stdin_fd);
451 TerminalState terminal_state(terminal);
453 if (terminal.IsATerminal()) {
455 llvm::consumeError(terminal.SetCanonical(
false));
456 llvm::consumeError(terminal.SetEcho(
true));
459 ScriptInterpreterPythonImpl::Locker locker(
461 ScriptInterpreterPythonImpl::Locker::AcquireLock |
462 ScriptInterpreterPythonImpl::Locker::InitSession |
463 ScriptInterpreterPythonImpl::Locker::InitGlobals,
464 ScriptInterpreterPythonImpl::Locker::FreeAcquiredLock |
465 ScriptInterpreterPythonImpl::Locker::TearDownSession);
480 StreamString run_string;
481 run_string.Printf(
"run_python_interpreter (%s)",
482 m_python->GetDictionaryName());
483 PyRun_SimpleString(run_string.GetData());
489 void Cancel()
override {}
491 bool Interrupt()
override {
return m_python->Interrupt(); }
493 void GotEOF()
override {}
496 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 ...
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
A class that represents a running process on the host machine.
ScriptedCommandSynchronicity
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::Stream > StreamSP
std::shared_ptr< lldb_private::Breakpoint > BreakpointSP
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