10b57cec5SDimitry Andric //===-- ScriptInterpreterPythonImpl.h ---------------------------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 9480093f4SDimitry Andric #include "lldb/Host/Config.h" 100b57cec5SDimitry Andric 11480093f4SDimitry Andric #if LLDB_ENABLE_PYTHON 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "lldb-python.h" 140b57cec5SDimitry Andric 150b57cec5SDimitry Andric #include "PythonDataObjects.h" 160b57cec5SDimitry Andric #include "ScriptInterpreterPython.h" 170b57cec5SDimitry Andric 180b57cec5SDimitry Andric #include "lldb/Host/Terminal.h" 190b57cec5SDimitry Andric #include "lldb/Utility/StreamString.h" 200b57cec5SDimitry Andric 210b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 220b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 230b57cec5SDimitry Andric 240b57cec5SDimitry Andric namespace lldb_private { 250b57cec5SDimitry Andric class IOHandlerPythonInterpreter; 260b57cec5SDimitry Andric class ScriptInterpreterPythonImpl : public ScriptInterpreterPython { 270b57cec5SDimitry Andric public: 280b57cec5SDimitry Andric friend class IOHandlerPythonInterpreter; 290b57cec5SDimitry Andric 300b57cec5SDimitry Andric ScriptInterpreterPythonImpl(Debugger &debugger); 310b57cec5SDimitry Andric 320b57cec5SDimitry Andric ~ScriptInterpreterPythonImpl() override; 330b57cec5SDimitry Andric 340b57cec5SDimitry Andric bool Interrupt() override; 350b57cec5SDimitry Andric 360b57cec5SDimitry Andric bool ExecuteOneLine( 370b57cec5SDimitry Andric llvm::StringRef command, CommandReturnObject *result, 380b57cec5SDimitry Andric const ExecuteScriptOptions &options = ExecuteScriptOptions()) override; 390b57cec5SDimitry Andric 400b57cec5SDimitry Andric void ExecuteInterpreterLoop() override; 410b57cec5SDimitry Andric 420b57cec5SDimitry Andric bool ExecuteOneLineWithReturn( 430b57cec5SDimitry Andric llvm::StringRef in_string, 440b57cec5SDimitry Andric ScriptInterpreter::ScriptReturnType return_type, void *ret_value, 450b57cec5SDimitry Andric const ExecuteScriptOptions &options = ExecuteScriptOptions()) override; 460b57cec5SDimitry Andric 470b57cec5SDimitry Andric lldb_private::Status ExecuteMultipleLines( 480b57cec5SDimitry Andric const char *in_string, 490b57cec5SDimitry Andric const ExecuteScriptOptions &options = ExecuteScriptOptions()) override; 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric Status 520b57cec5SDimitry Andric ExportFunctionDefinitionToInterpreter(StringList &function_def) override; 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric bool GenerateTypeScriptFunction(StringList &input, std::string &output, 550b57cec5SDimitry Andric const void *name_token = nullptr) override; 560b57cec5SDimitry Andric 570b57cec5SDimitry Andric bool GenerateTypeSynthClass(StringList &input, std::string &output, 580b57cec5SDimitry Andric const void *name_token = nullptr) override; 590b57cec5SDimitry Andric 600b57cec5SDimitry Andric bool GenerateTypeSynthClass(const char *oneliner, std::string &output, 610b57cec5SDimitry Andric const void *name_token = nullptr) override; 620b57cec5SDimitry Andric 630b57cec5SDimitry Andric // use this if the function code is just a one-liner script 640b57cec5SDimitry Andric bool GenerateTypeScriptFunction(const char *oneliner, std::string &output, 650b57cec5SDimitry Andric const void *name_token = nullptr) override; 660b57cec5SDimitry Andric 670b57cec5SDimitry Andric bool GenerateScriptAliasFunction(StringList &input, 680b57cec5SDimitry Andric std::string &output) override; 690b57cec5SDimitry Andric 700b57cec5SDimitry Andric StructuredData::ObjectSP 710b57cec5SDimitry Andric CreateSyntheticScriptedProvider(const char *class_name, 720b57cec5SDimitry Andric lldb::ValueObjectSP valobj) override; 730b57cec5SDimitry Andric 740b57cec5SDimitry Andric StructuredData::GenericSP 750b57cec5SDimitry Andric CreateScriptCommandObject(const char *class_name) override; 760b57cec5SDimitry Andric 770b57cec5SDimitry Andric StructuredData::ObjectSP 780b57cec5SDimitry Andric CreateScriptedThreadPlan(const char *class_name, 799dba64beSDimitry Andric StructuredDataImpl *args_data, 809dba64beSDimitry Andric std::string &error_str, 810b57cec5SDimitry Andric lldb::ThreadPlanSP thread_plan) override; 820b57cec5SDimitry Andric 830b57cec5SDimitry Andric bool ScriptedThreadPlanExplainsStop(StructuredData::ObjectSP implementor_sp, 840b57cec5SDimitry Andric Event *event, 850b57cec5SDimitry Andric bool &script_error) override; 860b57cec5SDimitry Andric 870b57cec5SDimitry Andric bool ScriptedThreadPlanShouldStop(StructuredData::ObjectSP implementor_sp, 880b57cec5SDimitry Andric Event *event, bool &script_error) override; 890b57cec5SDimitry Andric 900b57cec5SDimitry Andric bool ScriptedThreadPlanIsStale(StructuredData::ObjectSP implementor_sp, 910b57cec5SDimitry Andric bool &script_error) override; 920b57cec5SDimitry Andric 930b57cec5SDimitry Andric lldb::StateType 940b57cec5SDimitry Andric ScriptedThreadPlanGetRunState(StructuredData::ObjectSP implementor_sp, 950b57cec5SDimitry Andric bool &script_error) override; 960b57cec5SDimitry Andric 970b57cec5SDimitry Andric StructuredData::GenericSP 980b57cec5SDimitry Andric CreateScriptedBreakpointResolver(const char *class_name, 990b57cec5SDimitry Andric StructuredDataImpl *args_data, 1000b57cec5SDimitry Andric lldb::BreakpointSP &bkpt_sp) override; 1010b57cec5SDimitry Andric bool ScriptedBreakpointResolverSearchCallback( 1020b57cec5SDimitry Andric StructuredData::GenericSP implementor_sp, 1030b57cec5SDimitry Andric SymbolContext *sym_ctx) override; 1040b57cec5SDimitry Andric 1050b57cec5SDimitry Andric lldb::SearchDepth ScriptedBreakpointResolverSearchDepth( 1060b57cec5SDimitry Andric StructuredData::GenericSP implementor_sp) override; 1070b57cec5SDimitry Andric 1080b57cec5SDimitry Andric StructuredData::GenericSP 1090b57cec5SDimitry Andric CreateFrameRecognizer(const char *class_name) override; 1100b57cec5SDimitry Andric 1110b57cec5SDimitry Andric lldb::ValueObjectListSP 1120b57cec5SDimitry Andric GetRecognizedArguments(const StructuredData::ObjectSP &implementor, 1130b57cec5SDimitry Andric lldb::StackFrameSP frame_sp) override; 1140b57cec5SDimitry Andric 1150b57cec5SDimitry Andric StructuredData::GenericSP 1160b57cec5SDimitry Andric OSPlugin_CreatePluginObject(const char *class_name, 1170b57cec5SDimitry Andric lldb::ProcessSP process_sp) override; 1180b57cec5SDimitry Andric 1190b57cec5SDimitry Andric StructuredData::DictionarySP 1200b57cec5SDimitry Andric OSPlugin_RegisterInfo(StructuredData::ObjectSP os_plugin_object_sp) override; 1210b57cec5SDimitry Andric 1220b57cec5SDimitry Andric StructuredData::ArraySP 1230b57cec5SDimitry Andric OSPlugin_ThreadsInfo(StructuredData::ObjectSP os_plugin_object_sp) override; 1240b57cec5SDimitry Andric 1250b57cec5SDimitry Andric StructuredData::StringSP 1260b57cec5SDimitry Andric OSPlugin_RegisterContextData(StructuredData::ObjectSP os_plugin_object_sp, 1270b57cec5SDimitry Andric lldb::tid_t thread_id) override; 1280b57cec5SDimitry Andric 1290b57cec5SDimitry Andric StructuredData::DictionarySP 1300b57cec5SDimitry Andric OSPlugin_CreateThread(StructuredData::ObjectSP os_plugin_object_sp, 1310b57cec5SDimitry Andric lldb::tid_t tid, lldb::addr_t context) override; 1320b57cec5SDimitry Andric 1330b57cec5SDimitry Andric StructuredData::ObjectSP 1340b57cec5SDimitry Andric LoadPluginModule(const FileSpec &file_spec, 1350b57cec5SDimitry Andric lldb_private::Status &error) override; 1360b57cec5SDimitry Andric 1370b57cec5SDimitry Andric StructuredData::DictionarySP 1380b57cec5SDimitry Andric GetDynamicSettings(StructuredData::ObjectSP plugin_module_sp, Target *target, 1390b57cec5SDimitry Andric const char *setting_name, 1400b57cec5SDimitry Andric lldb_private::Status &error) override; 1410b57cec5SDimitry Andric 1420b57cec5SDimitry Andric size_t CalculateNumChildren(const StructuredData::ObjectSP &implementor, 1430b57cec5SDimitry Andric uint32_t max) override; 1440b57cec5SDimitry Andric 1450b57cec5SDimitry Andric lldb::ValueObjectSP 1460b57cec5SDimitry Andric GetChildAtIndex(const StructuredData::ObjectSP &implementor, 1470b57cec5SDimitry Andric uint32_t idx) override; 1480b57cec5SDimitry Andric 1490b57cec5SDimitry Andric int GetIndexOfChildWithName(const StructuredData::ObjectSP &implementor, 1500b57cec5SDimitry Andric const char *child_name) override; 1510b57cec5SDimitry Andric 1520b57cec5SDimitry Andric bool UpdateSynthProviderInstance( 1530b57cec5SDimitry Andric const StructuredData::ObjectSP &implementor) override; 1540b57cec5SDimitry Andric 1550b57cec5SDimitry Andric bool MightHaveChildrenSynthProviderInstance( 1560b57cec5SDimitry Andric const StructuredData::ObjectSP &implementor) override; 1570b57cec5SDimitry Andric 1580b57cec5SDimitry Andric lldb::ValueObjectSP 1590b57cec5SDimitry Andric GetSyntheticValue(const StructuredData::ObjectSP &implementor) override; 1600b57cec5SDimitry Andric 1610b57cec5SDimitry Andric ConstString 1620b57cec5SDimitry Andric GetSyntheticTypeName(const StructuredData::ObjectSP &implementor) override; 1630b57cec5SDimitry Andric 1640b57cec5SDimitry Andric bool 1650b57cec5SDimitry Andric RunScriptBasedCommand(const char *impl_function, llvm::StringRef args, 1660b57cec5SDimitry Andric ScriptedCommandSynchronicity synchronicity, 1670b57cec5SDimitry Andric lldb_private::CommandReturnObject &cmd_retobj, 1680b57cec5SDimitry Andric Status &error, 1690b57cec5SDimitry Andric const lldb_private::ExecutionContext &exe_ctx) override; 1700b57cec5SDimitry Andric 1710b57cec5SDimitry Andric bool RunScriptBasedCommand( 1720b57cec5SDimitry Andric StructuredData::GenericSP impl_obj_sp, llvm::StringRef args, 1730b57cec5SDimitry Andric ScriptedCommandSynchronicity synchronicity, 1740b57cec5SDimitry Andric lldb_private::CommandReturnObject &cmd_retobj, Status &error, 1750b57cec5SDimitry Andric const lldb_private::ExecutionContext &exe_ctx) override; 1760b57cec5SDimitry Andric 1770b57cec5SDimitry Andric Status GenerateFunction(const char *signature, 1780b57cec5SDimitry Andric const StringList &input) override; 1790b57cec5SDimitry Andric 180480093f4SDimitry Andric Status GenerateBreakpointCommandCallbackData( 181480093f4SDimitry Andric StringList &input, 182480093f4SDimitry Andric std::string &output, 183480093f4SDimitry Andric bool has_extra_args) override; 1840b57cec5SDimitry Andric 1850b57cec5SDimitry Andric bool GenerateWatchpointCommandCallbackData(StringList &input, 1860b57cec5SDimitry Andric std::string &output) override; 1870b57cec5SDimitry Andric 1880b57cec5SDimitry Andric bool GetScriptedSummary(const char *function_name, lldb::ValueObjectSP valobj, 1890b57cec5SDimitry Andric StructuredData::ObjectSP &callee_wrapper_sp, 1900b57cec5SDimitry Andric const TypeSummaryOptions &options, 1910b57cec5SDimitry Andric std::string &retval) override; 1920b57cec5SDimitry Andric 1930b57cec5SDimitry Andric bool GetDocumentationForItem(const char *item, std::string &dest) override; 1940b57cec5SDimitry Andric 1950b57cec5SDimitry Andric bool GetShortHelpForCommandObject(StructuredData::GenericSP cmd_obj_sp, 1960b57cec5SDimitry Andric std::string &dest) override; 1970b57cec5SDimitry Andric 1980b57cec5SDimitry Andric uint32_t 1990b57cec5SDimitry Andric GetFlagsForCommandObject(StructuredData::GenericSP cmd_obj_sp) override; 2000b57cec5SDimitry Andric 2010b57cec5SDimitry Andric bool GetLongHelpForCommandObject(StructuredData::GenericSP cmd_obj_sp, 2020b57cec5SDimitry Andric std::string &dest) override; 2030b57cec5SDimitry Andric 2040b57cec5SDimitry Andric bool CheckObjectExists(const char *name) override { 2050b57cec5SDimitry Andric if (!name || !name[0]) 2060b57cec5SDimitry Andric return false; 2070b57cec5SDimitry Andric std::string temp; 2080b57cec5SDimitry Andric return GetDocumentationForItem(name, temp); 2090b57cec5SDimitry Andric } 2100b57cec5SDimitry Andric 2110b57cec5SDimitry Andric bool RunScriptFormatKeyword(const char *impl_function, Process *process, 2120b57cec5SDimitry Andric std::string &output, Status &error) override; 2130b57cec5SDimitry Andric 2140b57cec5SDimitry Andric bool RunScriptFormatKeyword(const char *impl_function, Thread *thread, 2150b57cec5SDimitry Andric std::string &output, Status &error) override; 2160b57cec5SDimitry Andric 2170b57cec5SDimitry Andric bool RunScriptFormatKeyword(const char *impl_function, Target *target, 2180b57cec5SDimitry Andric std::string &output, Status &error) override; 2190b57cec5SDimitry Andric 2200b57cec5SDimitry Andric bool RunScriptFormatKeyword(const char *impl_function, StackFrame *frame, 2210b57cec5SDimitry Andric std::string &output, Status &error) override; 2220b57cec5SDimitry Andric 2230b57cec5SDimitry Andric bool RunScriptFormatKeyword(const char *impl_function, ValueObject *value, 2240b57cec5SDimitry Andric std::string &output, Status &error) override; 2250b57cec5SDimitry Andric 2260b57cec5SDimitry Andric bool 227480093f4SDimitry Andric LoadScriptingModule(const char *filename, bool init_session, 2280b57cec5SDimitry Andric lldb_private::Status &error, 2290b57cec5SDimitry Andric StructuredData::ObjectSP *module_sp = nullptr) override; 2300b57cec5SDimitry Andric 2310b57cec5SDimitry Andric bool IsReservedWord(const char *word) override; 2320b57cec5SDimitry Andric 2330b57cec5SDimitry Andric std::unique_ptr<ScriptInterpreterLocker> AcquireInterpreterLock() override; 2340b57cec5SDimitry Andric 2350b57cec5SDimitry Andric void CollectDataForBreakpointCommandCallback( 2360b57cec5SDimitry Andric std::vector<BreakpointOptions *> &bp_options_vec, 2370b57cec5SDimitry Andric CommandReturnObject &result) override; 2380b57cec5SDimitry Andric 2390b57cec5SDimitry Andric void 2400b57cec5SDimitry Andric CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options, 2410b57cec5SDimitry Andric CommandReturnObject &result) override; 2420b57cec5SDimitry Andric 2430b57cec5SDimitry Andric /// Set the callback body text into the callback for the breakpoint. 2440b57cec5SDimitry Andric Status SetBreakpointCommandCallback(BreakpointOptions *bp_options, 2450b57cec5SDimitry Andric const char *callback_body) override; 2460b57cec5SDimitry Andric 247480093f4SDimitry Andric Status SetBreakpointCommandCallbackFunction( 248480093f4SDimitry Andric BreakpointOptions *bp_options, 249480093f4SDimitry Andric const char *function_name, 250480093f4SDimitry Andric StructuredData::ObjectSP extra_args_sp) override; 2510b57cec5SDimitry Andric 2520b57cec5SDimitry Andric /// This one is for deserialization: 2530b57cec5SDimitry Andric Status SetBreakpointCommandCallback( 2540b57cec5SDimitry Andric BreakpointOptions *bp_options, 2550b57cec5SDimitry Andric std::unique_ptr<BreakpointOptions::CommandData> &data_up) override; 2560b57cec5SDimitry Andric 257480093f4SDimitry Andric Status SetBreakpointCommandCallback(BreakpointOptions *bp_options, 258480093f4SDimitry Andric const char *command_body_text, 259480093f4SDimitry Andric StructuredData::ObjectSP extra_args_sp, 260480093f4SDimitry Andric bool uses_extra_args); 261480093f4SDimitry Andric 2620b57cec5SDimitry Andric /// Set a one-liner as the callback for the watchpoint. 2630b57cec5SDimitry Andric void SetWatchpointCommandCallback(WatchpointOptions *wp_options, 2640b57cec5SDimitry Andric const char *oneliner) override; 2650b57cec5SDimitry Andric 2660b57cec5SDimitry Andric const char *GetDictionaryName() { return m_dictionary_name.c_str(); } 2670b57cec5SDimitry Andric 2680b57cec5SDimitry Andric PyThreadState *GetThreadState() { return m_command_thread_state; } 2690b57cec5SDimitry Andric 2700b57cec5SDimitry Andric void SetThreadState(PyThreadState *s) { 2710b57cec5SDimitry Andric if (s) 2720b57cec5SDimitry Andric m_command_thread_state = s; 2730b57cec5SDimitry Andric } 2740b57cec5SDimitry Andric 2750b57cec5SDimitry Andric // IOHandlerDelegate 2760b57cec5SDimitry Andric void IOHandlerActivated(IOHandler &io_handler, bool interactive) override; 2770b57cec5SDimitry Andric 2780b57cec5SDimitry Andric void IOHandlerInputComplete(IOHandler &io_handler, 2790b57cec5SDimitry Andric std::string &data) override; 2800b57cec5SDimitry Andric 2810b57cec5SDimitry Andric static lldb::ScriptInterpreterSP CreateInstance(Debugger &debugger); 2820b57cec5SDimitry Andric 2830b57cec5SDimitry Andric // PluginInterface protocol 2840b57cec5SDimitry Andric lldb_private::ConstString GetPluginName() override; 2850b57cec5SDimitry Andric 2860b57cec5SDimitry Andric uint32_t GetPluginVersion() override; 2870b57cec5SDimitry Andric 2880b57cec5SDimitry Andric class Locker : public ScriptInterpreterLocker { 2890b57cec5SDimitry Andric public: 2900b57cec5SDimitry Andric enum OnEntry { 2910b57cec5SDimitry Andric AcquireLock = 0x0001, 2920b57cec5SDimitry Andric InitSession = 0x0002, 2930b57cec5SDimitry Andric InitGlobals = 0x0004, 2940b57cec5SDimitry Andric NoSTDIN = 0x0008 2950b57cec5SDimitry Andric }; 2960b57cec5SDimitry Andric 2970b57cec5SDimitry Andric enum OnLeave { 2980b57cec5SDimitry Andric FreeLock = 0x0001, 2990b57cec5SDimitry Andric FreeAcquiredLock = 0x0002, // do not free the lock if we already held it 3000b57cec5SDimitry Andric // when calling constructor 3010b57cec5SDimitry Andric TearDownSession = 0x0004 3020b57cec5SDimitry Andric }; 3030b57cec5SDimitry Andric 3049dba64beSDimitry Andric Locker(ScriptInterpreterPythonImpl *py_interpreter, 3050b57cec5SDimitry Andric uint16_t on_entry = AcquireLock | InitSession, 3069dba64beSDimitry Andric uint16_t on_leave = FreeLock | TearDownSession, 3079dba64beSDimitry Andric lldb::FileSP in = nullptr, lldb::FileSP out = nullptr, 3089dba64beSDimitry Andric lldb::FileSP err = nullptr); 3090b57cec5SDimitry Andric 3100b57cec5SDimitry Andric ~Locker() override; 3110b57cec5SDimitry Andric 3120b57cec5SDimitry Andric private: 3130b57cec5SDimitry Andric bool DoAcquireLock(); 3140b57cec5SDimitry Andric 3159dba64beSDimitry Andric bool DoInitSession(uint16_t on_entry_flags, lldb::FileSP in, 3169dba64beSDimitry Andric lldb::FileSP out, lldb::FileSP err); 3170b57cec5SDimitry Andric 3180b57cec5SDimitry Andric bool DoFreeLock(); 3190b57cec5SDimitry Andric 3200b57cec5SDimitry Andric bool DoTearDownSession(); 3210b57cec5SDimitry Andric 3220b57cec5SDimitry Andric bool m_teardown_session; 3230b57cec5SDimitry Andric ScriptInterpreterPythonImpl *m_python_interpreter; 3240b57cec5SDimitry Andric PyGILState_STATE m_GILState; 3250b57cec5SDimitry Andric }; 3260b57cec5SDimitry Andric 3270b57cec5SDimitry Andric static bool BreakpointCallbackFunction(void *baton, 3280b57cec5SDimitry Andric StoppointCallbackContext *context, 3290b57cec5SDimitry Andric lldb::user_id_t break_id, 3300b57cec5SDimitry Andric lldb::user_id_t break_loc_id); 3310b57cec5SDimitry Andric static bool WatchpointCallbackFunction(void *baton, 3320b57cec5SDimitry Andric StoppointCallbackContext *context, 3330b57cec5SDimitry Andric lldb::user_id_t watch_id); 3340b57cec5SDimitry Andric static void InitializePrivate(); 3350b57cec5SDimitry Andric 3360b57cec5SDimitry Andric class SynchronicityHandler { 3370b57cec5SDimitry Andric private: 3380b57cec5SDimitry Andric lldb::DebuggerSP m_debugger_sp; 3390b57cec5SDimitry Andric ScriptedCommandSynchronicity m_synch_wanted; 3400b57cec5SDimitry Andric bool m_old_asynch; 3410b57cec5SDimitry Andric 3420b57cec5SDimitry Andric public: 3430b57cec5SDimitry Andric SynchronicityHandler(lldb::DebuggerSP, ScriptedCommandSynchronicity); 3440b57cec5SDimitry Andric 3450b57cec5SDimitry Andric ~SynchronicityHandler(); 3460b57cec5SDimitry Andric }; 3470b57cec5SDimitry Andric 3480b57cec5SDimitry Andric enum class AddLocation { Beginning, End }; 3490b57cec5SDimitry Andric 3500b57cec5SDimitry Andric static void AddToSysPath(AddLocation location, std::string path); 3510b57cec5SDimitry Andric 3529dba64beSDimitry Andric bool EnterSession(uint16_t on_entry_flags, lldb::FileSP in, lldb::FileSP out, 3539dba64beSDimitry Andric lldb::FileSP err); 3540b57cec5SDimitry Andric 3550b57cec5SDimitry Andric void LeaveSession(); 3560b57cec5SDimitry Andric 3570b57cec5SDimitry Andric uint32_t IsExecutingPython() const { return m_lock_count > 0; } 3580b57cec5SDimitry Andric 3590b57cec5SDimitry Andric uint32_t IncrementLockCount() { return ++m_lock_count; } 3600b57cec5SDimitry Andric 3610b57cec5SDimitry Andric uint32_t DecrementLockCount() { 3620b57cec5SDimitry Andric if (m_lock_count > 0) 3630b57cec5SDimitry Andric --m_lock_count; 3640b57cec5SDimitry Andric return m_lock_count; 3650b57cec5SDimitry Andric } 3660b57cec5SDimitry Andric 3670b57cec5SDimitry Andric enum ActiveIOHandler { 3680b57cec5SDimitry Andric eIOHandlerNone, 3690b57cec5SDimitry Andric eIOHandlerBreakpoint, 3700b57cec5SDimitry Andric eIOHandlerWatchpoint 3710b57cec5SDimitry Andric }; 3720b57cec5SDimitry Andric 3739dba64beSDimitry Andric python::PythonModule &GetMainModule(); 3740b57cec5SDimitry Andric 3759dba64beSDimitry Andric python::PythonDictionary &GetSessionDictionary(); 3760b57cec5SDimitry Andric 3779dba64beSDimitry Andric python::PythonDictionary &GetSysModuleDictionary(); 3780b57cec5SDimitry Andric 379480093f4SDimitry Andric llvm::Expected<unsigned> GetMaxPositionalArgumentsForCallable( 380480093f4SDimitry Andric const llvm::StringRef &callable_name) override; 381480093f4SDimitry Andric 3820b57cec5SDimitry Andric bool GetEmbeddedInterpreterModuleObjects(); 3830b57cec5SDimitry Andric 3849dba64beSDimitry Andric bool SetStdHandle(lldb::FileSP file, const char *py_name, 3859dba64beSDimitry Andric python::PythonObject &save_file, const char *mode); 3860b57cec5SDimitry Andric 3879dba64beSDimitry Andric python::PythonObject m_saved_stdin; 3889dba64beSDimitry Andric python::PythonObject m_saved_stdout; 3899dba64beSDimitry Andric python::PythonObject m_saved_stderr; 3909dba64beSDimitry Andric python::PythonModule m_main_module; 3919dba64beSDimitry Andric python::PythonDictionary m_session_dict; 3929dba64beSDimitry Andric python::PythonDictionary m_sys_module_dict; 3939dba64beSDimitry Andric python::PythonObject m_run_one_line_function; 3949dba64beSDimitry Andric python::PythonObject m_run_one_line_str_global; 3950b57cec5SDimitry Andric std::string m_dictionary_name; 3960b57cec5SDimitry Andric ActiveIOHandler m_active_io_handler; 3970b57cec5SDimitry Andric bool m_session_is_active; 398*5ffd83dbSDimitry Andric bool m_pty_secondary_is_open; 3990b57cec5SDimitry Andric bool m_valid_session; 4000b57cec5SDimitry Andric uint32_t m_lock_count; 4010b57cec5SDimitry Andric PyThreadState *m_command_thread_state; 4020b57cec5SDimitry Andric }; 4030b57cec5SDimitry Andric 4040b57cec5SDimitry Andric class IOHandlerPythonInterpreter : public IOHandler { 4050b57cec5SDimitry Andric public: 4060b57cec5SDimitry Andric IOHandlerPythonInterpreter(Debugger &debugger, 4070b57cec5SDimitry Andric ScriptInterpreterPythonImpl *python) 4080b57cec5SDimitry Andric : IOHandler(debugger, IOHandler::Type::PythonInterpreter), 4090b57cec5SDimitry Andric m_python(python) {} 4100b57cec5SDimitry Andric 4110b57cec5SDimitry Andric ~IOHandlerPythonInterpreter() override {} 4120b57cec5SDimitry Andric 4130b57cec5SDimitry Andric ConstString GetControlSequence(char ch) override { 4140b57cec5SDimitry Andric if (ch == 'd') 4150b57cec5SDimitry Andric return ConstString("quit()\n"); 4160b57cec5SDimitry Andric return ConstString(); 4170b57cec5SDimitry Andric } 4180b57cec5SDimitry Andric 4190b57cec5SDimitry Andric void Run() override { 4200b57cec5SDimitry Andric if (m_python) { 4210b57cec5SDimitry Andric int stdin_fd = GetInputFD(); 4220b57cec5SDimitry Andric if (stdin_fd >= 0) { 4230b57cec5SDimitry Andric Terminal terminal(stdin_fd); 4240b57cec5SDimitry Andric TerminalState terminal_state; 4250b57cec5SDimitry Andric const bool is_a_tty = terminal.IsATerminal(); 4260b57cec5SDimitry Andric 4270b57cec5SDimitry Andric if (is_a_tty) { 4280b57cec5SDimitry Andric terminal_state.Save(stdin_fd, false); 4290b57cec5SDimitry Andric terminal.SetCanonical(false); 4300b57cec5SDimitry Andric terminal.SetEcho(true); 4310b57cec5SDimitry Andric } 4320b57cec5SDimitry Andric 4330b57cec5SDimitry Andric ScriptInterpreterPythonImpl::Locker locker( 4340b57cec5SDimitry Andric m_python, 4350b57cec5SDimitry Andric ScriptInterpreterPythonImpl::Locker::AcquireLock | 4360b57cec5SDimitry Andric ScriptInterpreterPythonImpl::Locker::InitSession | 4370b57cec5SDimitry Andric ScriptInterpreterPythonImpl::Locker::InitGlobals, 4380b57cec5SDimitry Andric ScriptInterpreterPythonImpl::Locker::FreeAcquiredLock | 4390b57cec5SDimitry Andric ScriptInterpreterPythonImpl::Locker::TearDownSession); 4400b57cec5SDimitry Andric 4410b57cec5SDimitry Andric // The following call drops into the embedded interpreter loop and 4420b57cec5SDimitry Andric // stays there until the user chooses to exit from the Python 4430b57cec5SDimitry Andric // interpreter. This embedded interpreter will, as any Python code that 4440b57cec5SDimitry Andric // performs I/O, unlock the GIL before a system call that can hang, and 4450b57cec5SDimitry Andric // lock it when the syscall has returned. 4460b57cec5SDimitry Andric 4470b57cec5SDimitry Andric // We need to surround the call to the embedded interpreter with calls 4480b57cec5SDimitry Andric // to PyGILState_Ensure and PyGILState_Release (using the Locker 4490b57cec5SDimitry Andric // above). This is because Python has a global lock which must be held 4500b57cec5SDimitry Andric // whenever we want to touch any Python objects. Otherwise, if the user 4510b57cec5SDimitry Andric // calls Python code, the interpreter state will be off, and things 4520b57cec5SDimitry Andric // could hang (it's happened before). 4530b57cec5SDimitry Andric 4540b57cec5SDimitry Andric StreamString run_string; 4550b57cec5SDimitry Andric run_string.Printf("run_python_interpreter (%s)", 4560b57cec5SDimitry Andric m_python->GetDictionaryName()); 4570b57cec5SDimitry Andric PyRun_SimpleString(run_string.GetData()); 4580b57cec5SDimitry Andric 4590b57cec5SDimitry Andric if (is_a_tty) 4600b57cec5SDimitry Andric terminal_state.Restore(); 4610b57cec5SDimitry Andric } 4620b57cec5SDimitry Andric } 4630b57cec5SDimitry Andric SetIsDone(true); 4640b57cec5SDimitry Andric } 4650b57cec5SDimitry Andric 4660b57cec5SDimitry Andric void Cancel() override {} 4670b57cec5SDimitry Andric 4680b57cec5SDimitry Andric bool Interrupt() override { return m_python->Interrupt(); } 4690b57cec5SDimitry Andric 4700b57cec5SDimitry Andric void GotEOF() override {} 4710b57cec5SDimitry Andric 4720b57cec5SDimitry Andric protected: 4730b57cec5SDimitry Andric ScriptInterpreterPythonImpl *m_python; 4740b57cec5SDimitry Andric }; 4750b57cec5SDimitry Andric 4760b57cec5SDimitry Andric } // namespace lldb_private 4770b57cec5SDimitry Andric 4780b57cec5SDimitry Andric #endif 479