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 CreateScriptedThreadPlan(
const char *class_name,
82 const StructuredDataImpl &args_data,
83 std::string &error_str,
86 StructuredData::ObjectSP
87 CreateStructuredDataFromScriptObject(ScriptObject obj)
override;
89 bool ScriptedThreadPlanExplainsStop(StructuredData::ObjectSP implementor_sp,
91 bool &script_error)
override;
93 bool ScriptedThreadPlanShouldStop(StructuredData::ObjectSP implementor_sp,
94 Event *event,
bool &script_error)
override;
96 bool ScriptedThreadPlanIsStale(StructuredData::ObjectSP implementor_sp,
97 bool &script_error)
override;
100 ScriptedThreadPlanGetRunState(StructuredData::ObjectSP implementor_sp,
101 bool &script_error)
override;
104 ScriptedThreadPlanGetStopDescription(StructuredData::ObjectSP implementor_sp,
106 bool &script_error)
override;
108 StructuredData::GenericSP
109 CreateScriptedBreakpointResolver(
const char *class_name,
110 const StructuredDataImpl &args_data,
112 bool ScriptedBreakpointResolverSearchCallback(
113 StructuredData::GenericSP implementor_sp,
114 SymbolContext *sym_ctx)
override;
117 StructuredData::GenericSP implementor_sp)
override;
119 StructuredData::GenericSP
120 CreateScriptedStopHook(
lldb::TargetSP target_sp,
const char *class_name,
121 const StructuredDataImpl &args_data,
122 Status &
error)
override;
124 bool ScriptedStopHookHandleStop(StructuredData::GenericSP implementor_sp,
125 ExecutionContext &exc_ctx,
128 StructuredData::GenericSP
129 CreateFrameRecognizer(
const char *class_name)
override;
132 GetRecognizedArguments(
const StructuredData::ObjectSP &implementor,
141 StructuredData::ObjectSP
142 LoadPluginModule(
const FileSpec &file_spec,
145 StructuredData::DictionarySP
146 GetDynamicSettings(StructuredData::ObjectSP plugin_module_sp, Target *target,
147 const char *setting_name,
151 uint32_t max)
override;
154 GetChildAtIndex(
const StructuredData::ObjectSP &implementor,
155 uint32_t idx)
override;
157 int GetIndexOfChildWithName(
const StructuredData::ObjectSP &implementor,
158 const char *child_name)
override;
160 bool UpdateSynthProviderInstance(
161 const StructuredData::ObjectSP &implementor)
override;
163 bool MightHaveChildrenSynthProviderInstance(
164 const StructuredData::ObjectSP &implementor)
override;
167 GetSyntheticValue(
const StructuredData::ObjectSP &implementor)
override;
170 GetSyntheticTypeName(
const StructuredData::ObjectSP &implementor)
override;
173 RunScriptBasedCommand(
const char *impl_function, llvm::StringRef args,
174 ScriptedCommandSynchronicity synchronicity,
179 bool RunScriptBasedCommand(
180 StructuredData::GenericSP impl_obj_sp, llvm::StringRef args,
181 ScriptedCommandSynchronicity synchronicity,
185 virtual bool RunScriptBasedParsedCommand(
186 StructuredData::GenericSP impl_obj_sp, Args& args,
187 ScriptedCommandSynchronicity synchronicity,
192 Status GenerateFunction(
const char *signature,
const StringList &input,
193 bool is_callback)
override;
195 Status GenerateBreakpointCommandCallbackData(StringList &input,
198 bool is_callback)
override;
200 bool GenerateWatchpointCommandCallbackData(StringList &input,
202 bool is_callback)
override;
205 StructuredData::ObjectSP &callee_wrapper_sp,
206 const TypeSummaryOptions &options,
207 std::string &retval)
override;
209 bool FormatterCallbackFunction(
const char *function_name,
212 bool GetDocumentationForItem(
const char *item, std::string &dest)
override;
214 bool GetShortHelpForCommandObject(StructuredData::GenericSP cmd_obj_sp,
215 std::string &dest)
override;
218 GetFlagsForCommandObject(StructuredData::GenericSP cmd_obj_sp)
override;
220 bool GetLongHelpForCommandObject(StructuredData::GenericSP cmd_obj_sp,
221 std::string &dest)
override;
223 StructuredData::ObjectSP
224 GetOptionsForCommandObject(StructuredData::GenericSP cmd_obj_sp)
override;
226 StructuredData::ObjectSP
227 GetArgumentsForCommandObject(StructuredData::GenericSP cmd_obj_sp)
override;
229 bool SetOptionValueForCommandObject(StructuredData::GenericSP cmd_obj_sp,
230 ExecutionContext *exe_ctx,
231 llvm::StringRef long_option,
232 llvm::StringRef value)
override;
234 void OptionParsingStartedForCommandObject(
235 StructuredData::GenericSP cmd_obj_sp)
override;
237 bool CheckObjectExists(
const char *name)
override {
238 if (!name || !name[0])
241 return GetDocumentationForItem(name, temp);
245 std::string &output, Status &
error)
override;
248 std::string &output, Status &
error)
override;
251 std::string &output, Status &
error)
override;
254 std::string &output, Status &
error)
override;
257 std::string &output, Status &
error)
override;
259 bool LoadScriptingModule(
const char *filename,
260 const LoadScriptOptions &options,
262 StructuredData::ObjectSP *module_sp =
nullptr,
263 FileSpec extra_search_dir = {})
override;
265 bool IsReservedWord(
const char *word)
override;
267 std::unique_ptr<ScriptInterpreterLocker> AcquireInterpreterLock()
override;
269 void CollectDataForBreakpointCommandCallback(
270 std::vector<std::reference_wrapper<BreakpointOptions>> &bp_options_vec,
271 CommandReturnObject &result)
override;
274 CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options,
275 CommandReturnObject &result)
override;
278 Status SetBreakpointCommandCallback(BreakpointOptions &bp_options,
279 const char *callback_body,
280 bool is_callback)
override;
282 Status SetBreakpointCommandCallbackFunction(
283 BreakpointOptions &bp_options,
const char *function_name,
284 StructuredData::ObjectSP extra_args_sp)
override;
287 Status SetBreakpointCommandCallback(
288 BreakpointOptions &bp_options,
289 std::unique_ptr<BreakpointOptions::CommandData> &data_up)
override;
291 Status SetBreakpointCommandCallback(BreakpointOptions &bp_options,
292 const char *command_body_text,
293 StructuredData::ObjectSP extra_args_sp,
294 bool uses_extra_args,
298 void SetWatchpointCommandCallback(WatchpointOptions *wp_options,
299 const char *user_input,
300 bool is_callback)
override;
302 const char *GetDictionaryName() {
return m_dictionary_name.c_str(); }
304 PyThreadState *GetThreadState() {
return m_command_thread_state; }
306 void SetThreadState(PyThreadState *s) {
308 m_command_thread_state = s;
312 void IOHandlerActivated(IOHandler &io_handler,
bool interactive)
override;
314 void IOHandlerInputComplete(IOHandler &io_handler,
315 std::string &data)
override;
320 llvm::StringRef GetPluginName()
override {
return GetPluginNameStatic(); }
322 class Locker :
public ScriptInterpreterLocker {
325 AcquireLock = 0x0001,
326 InitSession = 0x0002,
327 InitGlobals = 0x0004,
333 FreeAcquiredLock = 0x0002,
335 TearDownSession = 0x0004
338 Locker(ScriptInterpreterPythonImpl *py_interpreter,
339 uint16_t on_entry = AcquireLock | InitSession,
340 uint16_t on_leave = FreeLock | TearDownSession,
347 bool DoAcquireLock();
349 bool DoInitSession(uint16_t on_entry_flags,
lldb::FileSP in,
354 bool DoTearDownSession();
356 bool m_teardown_session;
357 ScriptInterpreterPythonImpl *m_python_interpreter;
358 PyGILState_STATE m_GILState;
361 static bool BreakpointCallbackFunction(
void *baton,
362 StoppointCallbackContext *context,
365 static bool WatchpointCallbackFunction(
void *baton,
366 StoppointCallbackContext *context,
368 static void Initialize();
370 class SynchronicityHandler {
379 ~SynchronicityHandler();
382 enum class AddLocation { Beginning, End };
384 static void AddToSysPath(AddLocation location, std::string path);
391 uint32_t IsExecutingPython() {
392 std::lock_guard<std::mutex> guard(m_mutex);
393 return m_lock_count > 0;
396 uint32_t IncrementLockCount() {
397 std::lock_guard<std::mutex> guard(m_mutex);
398 return ++m_lock_count;
401 uint32_t DecrementLockCount() {
402 std::lock_guard<std::mutex> guard(m_mutex);
403 if (m_lock_count > 0)
414 python::PythonModule &GetMainModule();
416 python::PythonDictionary &GetSessionDictionary();
418 python::PythonDictionary &GetSysModuleDictionary();
420 llvm::Expected<unsigned> GetMaxPositionalArgumentsForCallable(
421 const llvm::StringRef &callable_name)
override;
423 bool GetEmbeddedInterpreterModuleObjects();
425 bool SetStdHandle(
lldb::FileSP file,
const char *py_name,
426 python::PythonObject &save_file,
const char *mode);
428 python::PythonObject m_saved_stdin;
429 python::PythonObject m_saved_stdout;
430 python::PythonObject m_saved_stderr;
431 python::PythonModule m_main_module;
432 python::PythonDictionary m_session_dict;
433 python::PythonDictionary m_sys_module_dict;
434 python::PythonObject m_run_one_line_function;
435 python::PythonObject m_run_one_line_str_global;
436 std::string m_dictionary_name;
438 bool m_session_is_active;
439 bool m_pty_secondary_is_open;
440 bool m_valid_session;
441 uint32_t m_lock_count;
443 PyThreadState *m_command_thread_state;
446class IOHandlerPythonInterpreter :
public IOHandler {
448 IOHandlerPythonInterpreter(Debugger &debugger,
449 ScriptInterpreterPythonImpl *python)
450 : IOHandler(debugger, IOHandler::
Type::PythonInterpreter),
453 ~IOHandlerPythonInterpreter()
override =
default;
455 llvm::StringRef GetControlSequence(
char ch)
override {
456 static constexpr llvm::StringLiteral control_sequence(
"quit()\n");
458 return control_sequence;
462 void Run()
override {
464 int stdin_fd = GetInputFD();
466 Terminal terminal(stdin_fd);
467 TerminalState terminal_state(terminal);
469 if (terminal.IsATerminal()) {
471 llvm::consumeError(terminal.SetCanonical(
false));
472 llvm::consumeError(terminal.SetEcho(
true));
475 ScriptInterpreterPythonImpl::Locker locker(
477 ScriptInterpreterPythonImpl::Locker::AcquireLock |
478 ScriptInterpreterPythonImpl::Locker::InitSession |
479 ScriptInterpreterPythonImpl::Locker::InitGlobals,
480 ScriptInterpreterPythonImpl::Locker::FreeAcquiredLock |
481 ScriptInterpreterPythonImpl::Locker::TearDownSession);
496 StreamString run_string;
497 run_string.Printf(
"run_python_interpreter (%s)",
498 m_python->GetDictionaryName());
499 PyRun_SimpleString(run_string.GetData());
505 void Cancel()
override {}
507 bool Interrupt()
override {
return m_python->Interrupt(); }
509 void GotEOF()
override {}
512 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 stream class that can stream formatted output to a file.
A class that represents a running process on the host machine.
ScriptedCommandSynchronicity
std::shared_ptr< lldb_private::ThreadPlan > ThreadPlanSP
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
std::shared_ptr< lldb_private::ScriptInterpreter > ScriptInterpreterSP
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
std::shared_ptr< lldb_private::OperatingSystemInterface > OperatingSystemInterfaceSP
StateType
Process and Thread States.
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