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 109*e8d8bef9SDimitry Andric CreateScriptedStopHook(lldb::TargetSP target_sp, const char *class_name, 110*e8d8bef9SDimitry Andric StructuredDataImpl *args_data, Status &error) override; 111*e8d8bef9SDimitry Andric 112*e8d8bef9SDimitry Andric bool ScriptedStopHookHandleStop(StructuredData::GenericSP implementor_sp, 113*e8d8bef9SDimitry Andric ExecutionContext &exc_ctx, 114*e8d8bef9SDimitry Andric lldb::StreamSP stream_sp) override; 115*e8d8bef9SDimitry Andric 116*e8d8bef9SDimitry Andric StructuredData::GenericSP 1170b57cec5SDimitry Andric CreateFrameRecognizer(const char *class_name) override; 1180b57cec5SDimitry Andric 1190b57cec5SDimitry Andric lldb::ValueObjectListSP 1200b57cec5SDimitry Andric GetRecognizedArguments(const StructuredData::ObjectSP &implementor, 1210b57cec5SDimitry Andric lldb::StackFrameSP frame_sp) override; 1220b57cec5SDimitry Andric 1230b57cec5SDimitry Andric StructuredData::GenericSP 1240b57cec5SDimitry Andric OSPlugin_CreatePluginObject(const char *class_name, 1250b57cec5SDimitry Andric lldb::ProcessSP process_sp) override; 1260b57cec5SDimitry Andric 1270b57cec5SDimitry Andric StructuredData::DictionarySP 1280b57cec5SDimitry Andric OSPlugin_RegisterInfo(StructuredData::ObjectSP os_plugin_object_sp) override; 1290b57cec5SDimitry Andric 1300b57cec5SDimitry Andric StructuredData::ArraySP 1310b57cec5SDimitry Andric OSPlugin_ThreadsInfo(StructuredData::ObjectSP os_plugin_object_sp) override; 1320b57cec5SDimitry Andric 1330b57cec5SDimitry Andric StructuredData::StringSP 1340b57cec5SDimitry Andric OSPlugin_RegisterContextData(StructuredData::ObjectSP os_plugin_object_sp, 1350b57cec5SDimitry Andric lldb::tid_t thread_id) override; 1360b57cec5SDimitry Andric 1370b57cec5SDimitry Andric StructuredData::DictionarySP 1380b57cec5SDimitry Andric OSPlugin_CreateThread(StructuredData::ObjectSP os_plugin_object_sp, 1390b57cec5SDimitry Andric lldb::tid_t tid, lldb::addr_t context) override; 1400b57cec5SDimitry Andric 1410b57cec5SDimitry Andric StructuredData::ObjectSP 1420b57cec5SDimitry Andric LoadPluginModule(const FileSpec &file_spec, 1430b57cec5SDimitry Andric lldb_private::Status &error) override; 1440b57cec5SDimitry Andric 1450b57cec5SDimitry Andric StructuredData::DictionarySP 1460b57cec5SDimitry Andric GetDynamicSettings(StructuredData::ObjectSP plugin_module_sp, Target *target, 1470b57cec5SDimitry Andric const char *setting_name, 1480b57cec5SDimitry Andric lldb_private::Status &error) override; 1490b57cec5SDimitry Andric 1500b57cec5SDimitry Andric size_t CalculateNumChildren(const StructuredData::ObjectSP &implementor, 1510b57cec5SDimitry Andric uint32_t max) override; 1520b57cec5SDimitry Andric 1530b57cec5SDimitry Andric lldb::ValueObjectSP 1540b57cec5SDimitry Andric GetChildAtIndex(const StructuredData::ObjectSP &implementor, 1550b57cec5SDimitry Andric uint32_t idx) override; 1560b57cec5SDimitry Andric 1570b57cec5SDimitry Andric int GetIndexOfChildWithName(const StructuredData::ObjectSP &implementor, 1580b57cec5SDimitry Andric const char *child_name) override; 1590b57cec5SDimitry Andric 1600b57cec5SDimitry Andric bool UpdateSynthProviderInstance( 1610b57cec5SDimitry Andric const StructuredData::ObjectSP &implementor) override; 1620b57cec5SDimitry Andric 1630b57cec5SDimitry Andric bool MightHaveChildrenSynthProviderInstance( 1640b57cec5SDimitry Andric const StructuredData::ObjectSP &implementor) override; 1650b57cec5SDimitry Andric 1660b57cec5SDimitry Andric lldb::ValueObjectSP 1670b57cec5SDimitry Andric GetSyntheticValue(const StructuredData::ObjectSP &implementor) override; 1680b57cec5SDimitry Andric 1690b57cec5SDimitry Andric ConstString 1700b57cec5SDimitry Andric GetSyntheticTypeName(const StructuredData::ObjectSP &implementor) override; 1710b57cec5SDimitry Andric 1720b57cec5SDimitry Andric bool 1730b57cec5SDimitry Andric RunScriptBasedCommand(const char *impl_function, llvm::StringRef args, 1740b57cec5SDimitry Andric ScriptedCommandSynchronicity synchronicity, 1750b57cec5SDimitry Andric lldb_private::CommandReturnObject &cmd_retobj, 1760b57cec5SDimitry Andric Status &error, 1770b57cec5SDimitry Andric const lldb_private::ExecutionContext &exe_ctx) override; 1780b57cec5SDimitry Andric 1790b57cec5SDimitry Andric bool RunScriptBasedCommand( 1800b57cec5SDimitry Andric StructuredData::GenericSP impl_obj_sp, llvm::StringRef args, 1810b57cec5SDimitry Andric ScriptedCommandSynchronicity synchronicity, 1820b57cec5SDimitry Andric lldb_private::CommandReturnObject &cmd_retobj, Status &error, 1830b57cec5SDimitry Andric const lldb_private::ExecutionContext &exe_ctx) override; 1840b57cec5SDimitry Andric 1850b57cec5SDimitry Andric Status GenerateFunction(const char *signature, 1860b57cec5SDimitry Andric const StringList &input) override; 1870b57cec5SDimitry Andric 188480093f4SDimitry Andric Status GenerateBreakpointCommandCallbackData( 189480093f4SDimitry Andric StringList &input, 190480093f4SDimitry Andric std::string &output, 191480093f4SDimitry Andric bool has_extra_args) override; 1920b57cec5SDimitry Andric 1930b57cec5SDimitry Andric bool GenerateWatchpointCommandCallbackData(StringList &input, 1940b57cec5SDimitry Andric std::string &output) override; 1950b57cec5SDimitry Andric 1960b57cec5SDimitry Andric bool GetScriptedSummary(const char *function_name, lldb::ValueObjectSP valobj, 1970b57cec5SDimitry Andric StructuredData::ObjectSP &callee_wrapper_sp, 1980b57cec5SDimitry Andric const TypeSummaryOptions &options, 1990b57cec5SDimitry Andric std::string &retval) override; 2000b57cec5SDimitry Andric 2010b57cec5SDimitry Andric bool GetDocumentationForItem(const char *item, std::string &dest) override; 2020b57cec5SDimitry Andric 2030b57cec5SDimitry Andric bool GetShortHelpForCommandObject(StructuredData::GenericSP cmd_obj_sp, 2040b57cec5SDimitry Andric std::string &dest) override; 2050b57cec5SDimitry Andric 2060b57cec5SDimitry Andric uint32_t 2070b57cec5SDimitry Andric GetFlagsForCommandObject(StructuredData::GenericSP cmd_obj_sp) override; 2080b57cec5SDimitry Andric 2090b57cec5SDimitry Andric bool GetLongHelpForCommandObject(StructuredData::GenericSP cmd_obj_sp, 2100b57cec5SDimitry Andric std::string &dest) override; 2110b57cec5SDimitry Andric 2120b57cec5SDimitry Andric bool CheckObjectExists(const char *name) override { 2130b57cec5SDimitry Andric if (!name || !name[0]) 2140b57cec5SDimitry Andric return false; 2150b57cec5SDimitry Andric std::string temp; 2160b57cec5SDimitry Andric return GetDocumentationForItem(name, temp); 2170b57cec5SDimitry Andric } 2180b57cec5SDimitry Andric 2190b57cec5SDimitry Andric bool RunScriptFormatKeyword(const char *impl_function, Process *process, 2200b57cec5SDimitry Andric std::string &output, Status &error) override; 2210b57cec5SDimitry Andric 2220b57cec5SDimitry Andric bool RunScriptFormatKeyword(const char *impl_function, Thread *thread, 2230b57cec5SDimitry Andric std::string &output, Status &error) override; 2240b57cec5SDimitry Andric 2250b57cec5SDimitry Andric bool RunScriptFormatKeyword(const char *impl_function, Target *target, 2260b57cec5SDimitry Andric std::string &output, Status &error) override; 2270b57cec5SDimitry Andric 2280b57cec5SDimitry Andric bool RunScriptFormatKeyword(const char *impl_function, StackFrame *frame, 2290b57cec5SDimitry Andric std::string &output, Status &error) override; 2300b57cec5SDimitry Andric 2310b57cec5SDimitry Andric bool RunScriptFormatKeyword(const char *impl_function, ValueObject *value, 2320b57cec5SDimitry Andric std::string &output, Status &error) override; 2330b57cec5SDimitry Andric 234*e8d8bef9SDimitry Andric bool LoadScriptingModule(const char *filename, bool init_session, 2350b57cec5SDimitry Andric lldb_private::Status &error, 236*e8d8bef9SDimitry Andric StructuredData::ObjectSP *module_sp = nullptr, 237*e8d8bef9SDimitry Andric FileSpec extra_search_dir = {}) override; 2380b57cec5SDimitry Andric 2390b57cec5SDimitry Andric bool IsReservedWord(const char *word) override; 2400b57cec5SDimitry Andric 2410b57cec5SDimitry Andric std::unique_ptr<ScriptInterpreterLocker> AcquireInterpreterLock() override; 2420b57cec5SDimitry Andric 2430b57cec5SDimitry Andric void CollectDataForBreakpointCommandCallback( 2440b57cec5SDimitry Andric std::vector<BreakpointOptions *> &bp_options_vec, 2450b57cec5SDimitry Andric CommandReturnObject &result) override; 2460b57cec5SDimitry Andric 2470b57cec5SDimitry Andric void 2480b57cec5SDimitry Andric CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options, 2490b57cec5SDimitry Andric CommandReturnObject &result) override; 2500b57cec5SDimitry Andric 2510b57cec5SDimitry Andric /// Set the callback body text into the callback for the breakpoint. 2520b57cec5SDimitry Andric Status SetBreakpointCommandCallback(BreakpointOptions *bp_options, 2530b57cec5SDimitry Andric const char *callback_body) override; 2540b57cec5SDimitry Andric 255480093f4SDimitry Andric Status SetBreakpointCommandCallbackFunction( 256480093f4SDimitry Andric BreakpointOptions *bp_options, 257480093f4SDimitry Andric const char *function_name, 258480093f4SDimitry Andric StructuredData::ObjectSP extra_args_sp) override; 2590b57cec5SDimitry Andric 2600b57cec5SDimitry Andric /// This one is for deserialization: 2610b57cec5SDimitry Andric Status SetBreakpointCommandCallback( 2620b57cec5SDimitry Andric BreakpointOptions *bp_options, 2630b57cec5SDimitry Andric std::unique_ptr<BreakpointOptions::CommandData> &data_up) override; 2640b57cec5SDimitry Andric 265480093f4SDimitry Andric Status SetBreakpointCommandCallback(BreakpointOptions *bp_options, 266480093f4SDimitry Andric const char *command_body_text, 267480093f4SDimitry Andric StructuredData::ObjectSP extra_args_sp, 268480093f4SDimitry Andric bool uses_extra_args); 269480093f4SDimitry Andric 2700b57cec5SDimitry Andric /// Set a one-liner as the callback for the watchpoint. 2710b57cec5SDimitry Andric void SetWatchpointCommandCallback(WatchpointOptions *wp_options, 2720b57cec5SDimitry Andric const char *oneliner) override; 2730b57cec5SDimitry Andric 2740b57cec5SDimitry Andric const char *GetDictionaryName() { return m_dictionary_name.c_str(); } 2750b57cec5SDimitry Andric 2760b57cec5SDimitry Andric PyThreadState *GetThreadState() { return m_command_thread_state; } 2770b57cec5SDimitry Andric 2780b57cec5SDimitry Andric void SetThreadState(PyThreadState *s) { 2790b57cec5SDimitry Andric if (s) 2800b57cec5SDimitry Andric m_command_thread_state = s; 2810b57cec5SDimitry Andric } 2820b57cec5SDimitry Andric 2830b57cec5SDimitry Andric // IOHandlerDelegate 2840b57cec5SDimitry Andric void IOHandlerActivated(IOHandler &io_handler, bool interactive) override; 2850b57cec5SDimitry Andric 2860b57cec5SDimitry Andric void IOHandlerInputComplete(IOHandler &io_handler, 2870b57cec5SDimitry Andric std::string &data) override; 2880b57cec5SDimitry Andric 2890b57cec5SDimitry Andric static lldb::ScriptInterpreterSP CreateInstance(Debugger &debugger); 2900b57cec5SDimitry Andric 2910b57cec5SDimitry Andric // PluginInterface protocol 2920b57cec5SDimitry Andric lldb_private::ConstString GetPluginName() override; 2930b57cec5SDimitry Andric 2940b57cec5SDimitry Andric uint32_t GetPluginVersion() override; 2950b57cec5SDimitry Andric 2960b57cec5SDimitry Andric class Locker : public ScriptInterpreterLocker { 2970b57cec5SDimitry Andric public: 2980b57cec5SDimitry Andric enum OnEntry { 2990b57cec5SDimitry Andric AcquireLock = 0x0001, 3000b57cec5SDimitry Andric InitSession = 0x0002, 3010b57cec5SDimitry Andric InitGlobals = 0x0004, 3020b57cec5SDimitry Andric NoSTDIN = 0x0008 3030b57cec5SDimitry Andric }; 3040b57cec5SDimitry Andric 3050b57cec5SDimitry Andric enum OnLeave { 3060b57cec5SDimitry Andric FreeLock = 0x0001, 3070b57cec5SDimitry Andric FreeAcquiredLock = 0x0002, // do not free the lock if we already held it 3080b57cec5SDimitry Andric // when calling constructor 3090b57cec5SDimitry Andric TearDownSession = 0x0004 3100b57cec5SDimitry Andric }; 3110b57cec5SDimitry Andric 3129dba64beSDimitry Andric Locker(ScriptInterpreterPythonImpl *py_interpreter, 3130b57cec5SDimitry Andric uint16_t on_entry = AcquireLock | InitSession, 3149dba64beSDimitry Andric uint16_t on_leave = FreeLock | TearDownSession, 3159dba64beSDimitry Andric lldb::FileSP in = nullptr, lldb::FileSP out = nullptr, 3169dba64beSDimitry Andric lldb::FileSP err = nullptr); 3170b57cec5SDimitry Andric 3180b57cec5SDimitry Andric ~Locker() override; 3190b57cec5SDimitry Andric 3200b57cec5SDimitry Andric private: 3210b57cec5SDimitry Andric bool DoAcquireLock(); 3220b57cec5SDimitry Andric 3239dba64beSDimitry Andric bool DoInitSession(uint16_t on_entry_flags, lldb::FileSP in, 3249dba64beSDimitry Andric lldb::FileSP out, lldb::FileSP err); 3250b57cec5SDimitry Andric 3260b57cec5SDimitry Andric bool DoFreeLock(); 3270b57cec5SDimitry Andric 3280b57cec5SDimitry Andric bool DoTearDownSession(); 3290b57cec5SDimitry Andric 3300b57cec5SDimitry Andric bool m_teardown_session; 3310b57cec5SDimitry Andric ScriptInterpreterPythonImpl *m_python_interpreter; 3320b57cec5SDimitry Andric PyGILState_STATE m_GILState; 3330b57cec5SDimitry Andric }; 3340b57cec5SDimitry Andric 3350b57cec5SDimitry Andric static bool BreakpointCallbackFunction(void *baton, 3360b57cec5SDimitry Andric StoppointCallbackContext *context, 3370b57cec5SDimitry Andric lldb::user_id_t break_id, 3380b57cec5SDimitry Andric lldb::user_id_t break_loc_id); 3390b57cec5SDimitry Andric static bool WatchpointCallbackFunction(void *baton, 3400b57cec5SDimitry Andric StoppointCallbackContext *context, 3410b57cec5SDimitry Andric lldb::user_id_t watch_id); 3420b57cec5SDimitry Andric static void InitializePrivate(); 3430b57cec5SDimitry Andric 3440b57cec5SDimitry Andric class SynchronicityHandler { 3450b57cec5SDimitry Andric private: 3460b57cec5SDimitry Andric lldb::DebuggerSP m_debugger_sp; 3470b57cec5SDimitry Andric ScriptedCommandSynchronicity m_synch_wanted; 3480b57cec5SDimitry Andric bool m_old_asynch; 3490b57cec5SDimitry Andric 3500b57cec5SDimitry Andric public: 3510b57cec5SDimitry Andric SynchronicityHandler(lldb::DebuggerSP, ScriptedCommandSynchronicity); 3520b57cec5SDimitry Andric 3530b57cec5SDimitry Andric ~SynchronicityHandler(); 3540b57cec5SDimitry Andric }; 3550b57cec5SDimitry Andric 3560b57cec5SDimitry Andric enum class AddLocation { Beginning, End }; 3570b57cec5SDimitry Andric 3580b57cec5SDimitry Andric static void AddToSysPath(AddLocation location, std::string path); 3590b57cec5SDimitry Andric 3609dba64beSDimitry Andric bool EnterSession(uint16_t on_entry_flags, lldb::FileSP in, lldb::FileSP out, 3619dba64beSDimitry Andric lldb::FileSP err); 3620b57cec5SDimitry Andric 3630b57cec5SDimitry Andric void LeaveSession(); 3640b57cec5SDimitry Andric 3650b57cec5SDimitry Andric uint32_t IsExecutingPython() const { return m_lock_count > 0; } 3660b57cec5SDimitry Andric 3670b57cec5SDimitry Andric uint32_t IncrementLockCount() { return ++m_lock_count; } 3680b57cec5SDimitry Andric 3690b57cec5SDimitry Andric uint32_t DecrementLockCount() { 3700b57cec5SDimitry Andric if (m_lock_count > 0) 3710b57cec5SDimitry Andric --m_lock_count; 3720b57cec5SDimitry Andric return m_lock_count; 3730b57cec5SDimitry Andric } 3740b57cec5SDimitry Andric 3750b57cec5SDimitry Andric enum ActiveIOHandler { 3760b57cec5SDimitry Andric eIOHandlerNone, 3770b57cec5SDimitry Andric eIOHandlerBreakpoint, 3780b57cec5SDimitry Andric eIOHandlerWatchpoint 3790b57cec5SDimitry Andric }; 3800b57cec5SDimitry Andric 3819dba64beSDimitry Andric python::PythonModule &GetMainModule(); 3820b57cec5SDimitry Andric 3839dba64beSDimitry Andric python::PythonDictionary &GetSessionDictionary(); 3840b57cec5SDimitry Andric 3859dba64beSDimitry Andric python::PythonDictionary &GetSysModuleDictionary(); 3860b57cec5SDimitry Andric 387480093f4SDimitry Andric llvm::Expected<unsigned> GetMaxPositionalArgumentsForCallable( 388480093f4SDimitry Andric const llvm::StringRef &callable_name) override; 389480093f4SDimitry Andric 3900b57cec5SDimitry Andric bool GetEmbeddedInterpreterModuleObjects(); 3910b57cec5SDimitry Andric 3929dba64beSDimitry Andric bool SetStdHandle(lldb::FileSP file, const char *py_name, 3939dba64beSDimitry Andric python::PythonObject &save_file, const char *mode); 3940b57cec5SDimitry Andric 3959dba64beSDimitry Andric python::PythonObject m_saved_stdin; 3969dba64beSDimitry Andric python::PythonObject m_saved_stdout; 3979dba64beSDimitry Andric python::PythonObject m_saved_stderr; 3989dba64beSDimitry Andric python::PythonModule m_main_module; 3999dba64beSDimitry Andric python::PythonDictionary m_session_dict; 4009dba64beSDimitry Andric python::PythonDictionary m_sys_module_dict; 4019dba64beSDimitry Andric python::PythonObject m_run_one_line_function; 4029dba64beSDimitry Andric python::PythonObject m_run_one_line_str_global; 4030b57cec5SDimitry Andric std::string m_dictionary_name; 4040b57cec5SDimitry Andric ActiveIOHandler m_active_io_handler; 4050b57cec5SDimitry Andric bool m_session_is_active; 4065ffd83dbSDimitry Andric bool m_pty_secondary_is_open; 4070b57cec5SDimitry Andric bool m_valid_session; 4080b57cec5SDimitry Andric uint32_t m_lock_count; 4090b57cec5SDimitry Andric PyThreadState *m_command_thread_state; 4100b57cec5SDimitry Andric }; 4110b57cec5SDimitry Andric 4120b57cec5SDimitry Andric class IOHandlerPythonInterpreter : public IOHandler { 4130b57cec5SDimitry Andric public: 4140b57cec5SDimitry Andric IOHandlerPythonInterpreter(Debugger &debugger, 4150b57cec5SDimitry Andric ScriptInterpreterPythonImpl *python) 4160b57cec5SDimitry Andric : IOHandler(debugger, IOHandler::Type::PythonInterpreter), 4170b57cec5SDimitry Andric m_python(python) {} 4180b57cec5SDimitry Andric 4190b57cec5SDimitry Andric ~IOHandlerPythonInterpreter() override {} 4200b57cec5SDimitry Andric 4210b57cec5SDimitry Andric ConstString GetControlSequence(char ch) override { 4220b57cec5SDimitry Andric if (ch == 'd') 4230b57cec5SDimitry Andric return ConstString("quit()\n"); 4240b57cec5SDimitry Andric return ConstString(); 4250b57cec5SDimitry Andric } 4260b57cec5SDimitry Andric 4270b57cec5SDimitry Andric void Run() override { 4280b57cec5SDimitry Andric if (m_python) { 4290b57cec5SDimitry Andric int stdin_fd = GetInputFD(); 4300b57cec5SDimitry Andric if (stdin_fd >= 0) { 4310b57cec5SDimitry Andric Terminal terminal(stdin_fd); 4320b57cec5SDimitry Andric TerminalState terminal_state; 4330b57cec5SDimitry Andric const bool is_a_tty = terminal.IsATerminal(); 4340b57cec5SDimitry Andric 4350b57cec5SDimitry Andric if (is_a_tty) { 4360b57cec5SDimitry Andric terminal_state.Save(stdin_fd, false); 4370b57cec5SDimitry Andric terminal.SetCanonical(false); 4380b57cec5SDimitry Andric terminal.SetEcho(true); 4390b57cec5SDimitry Andric } 4400b57cec5SDimitry Andric 4410b57cec5SDimitry Andric ScriptInterpreterPythonImpl::Locker locker( 4420b57cec5SDimitry Andric m_python, 4430b57cec5SDimitry Andric ScriptInterpreterPythonImpl::Locker::AcquireLock | 4440b57cec5SDimitry Andric ScriptInterpreterPythonImpl::Locker::InitSession | 4450b57cec5SDimitry Andric ScriptInterpreterPythonImpl::Locker::InitGlobals, 4460b57cec5SDimitry Andric ScriptInterpreterPythonImpl::Locker::FreeAcquiredLock | 4470b57cec5SDimitry Andric ScriptInterpreterPythonImpl::Locker::TearDownSession); 4480b57cec5SDimitry Andric 4490b57cec5SDimitry Andric // The following call drops into the embedded interpreter loop and 4500b57cec5SDimitry Andric // stays there until the user chooses to exit from the Python 4510b57cec5SDimitry Andric // interpreter. This embedded interpreter will, as any Python code that 4520b57cec5SDimitry Andric // performs I/O, unlock the GIL before a system call that can hang, and 4530b57cec5SDimitry Andric // lock it when the syscall has returned. 4540b57cec5SDimitry Andric 4550b57cec5SDimitry Andric // We need to surround the call to the embedded interpreter with calls 4560b57cec5SDimitry Andric // to PyGILState_Ensure and PyGILState_Release (using the Locker 4570b57cec5SDimitry Andric // above). This is because Python has a global lock which must be held 4580b57cec5SDimitry Andric // whenever we want to touch any Python objects. Otherwise, if the user 4590b57cec5SDimitry Andric // calls Python code, the interpreter state will be off, and things 4600b57cec5SDimitry Andric // could hang (it's happened before). 4610b57cec5SDimitry Andric 4620b57cec5SDimitry Andric StreamString run_string; 4630b57cec5SDimitry Andric run_string.Printf("run_python_interpreter (%s)", 4640b57cec5SDimitry Andric m_python->GetDictionaryName()); 4650b57cec5SDimitry Andric PyRun_SimpleString(run_string.GetData()); 4660b57cec5SDimitry Andric 4670b57cec5SDimitry Andric if (is_a_tty) 4680b57cec5SDimitry Andric terminal_state.Restore(); 4690b57cec5SDimitry Andric } 4700b57cec5SDimitry Andric } 4710b57cec5SDimitry Andric SetIsDone(true); 4720b57cec5SDimitry Andric } 4730b57cec5SDimitry Andric 4740b57cec5SDimitry Andric void Cancel() override {} 4750b57cec5SDimitry Andric 4760b57cec5SDimitry Andric bool Interrupt() override { return m_python->Interrupt(); } 4770b57cec5SDimitry Andric 4780b57cec5SDimitry Andric void GotEOF() override {} 4790b57cec5SDimitry Andric 4800b57cec5SDimitry Andric protected: 4810b57cec5SDimitry Andric ScriptInterpreterPythonImpl *m_python; 4820b57cec5SDimitry Andric }; 4830b57cec5SDimitry Andric 4840b57cec5SDimitry Andric } // namespace lldb_private 4850b57cec5SDimitry Andric 4860b57cec5SDimitry Andric #endif 487