1*0b57cec5SDimitry Andric //===-- ScriptInterpreterPythonImpl.h ---------------------------*- C++ -*-===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric 9480093f4SDimitry Andric #include "lldb/Host/Config.h" 10*0b57cec5SDimitry Andric 11480093f4SDimitry Andric #if LLDB_ENABLE_PYTHON 12*0b57cec5SDimitry Andric 13*0b57cec5SDimitry Andric #include "lldb-python.h" 14*0b57cec5SDimitry Andric 15*0b57cec5SDimitry Andric #include "PythonDataObjects.h" 16*0b57cec5SDimitry Andric #include "ScriptInterpreterPython.h" 17*0b57cec5SDimitry Andric 18*0b57cec5SDimitry Andric #include "lldb/Host/Terminal.h" 19*0b57cec5SDimitry Andric #include "lldb/Utility/StreamString.h" 20*0b57cec5SDimitry Andric 21*0b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 22*0b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 23*0b57cec5SDimitry Andric 24*0b57cec5SDimitry Andric namespace lldb_private { 25*0b57cec5SDimitry Andric class IOHandlerPythonInterpreter; 26*0b57cec5SDimitry Andric class ScriptInterpreterPythonImpl : public ScriptInterpreterPython { 27*0b57cec5SDimitry Andric public: 28*0b57cec5SDimitry Andric friend class IOHandlerPythonInterpreter; 29*0b57cec5SDimitry Andric 30*0b57cec5SDimitry Andric ScriptInterpreterPythonImpl(Debugger &debugger); 31*0b57cec5SDimitry Andric 32*0b57cec5SDimitry Andric ~ScriptInterpreterPythonImpl() override; 33*0b57cec5SDimitry Andric 34*0b57cec5SDimitry Andric bool Interrupt() override; 35*0b57cec5SDimitry Andric 36*0b57cec5SDimitry Andric bool ExecuteOneLine( 37*0b57cec5SDimitry Andric llvm::StringRef command, CommandReturnObject *result, 38*0b57cec5SDimitry Andric const ExecuteScriptOptions &options = ExecuteScriptOptions()) override; 39*0b57cec5SDimitry Andric 40*0b57cec5SDimitry Andric void ExecuteInterpreterLoop() override; 41*0b57cec5SDimitry Andric 42*0b57cec5SDimitry Andric bool ExecuteOneLineWithReturn( 43*0b57cec5SDimitry Andric llvm::StringRef in_string, 44*0b57cec5SDimitry Andric ScriptInterpreter::ScriptReturnType return_type, void *ret_value, 45*0b57cec5SDimitry Andric const ExecuteScriptOptions &options = ExecuteScriptOptions()) override; 46*0b57cec5SDimitry Andric 47*0b57cec5SDimitry Andric lldb_private::Status ExecuteMultipleLines( 48*0b57cec5SDimitry Andric const char *in_string, 49*0b57cec5SDimitry Andric const ExecuteScriptOptions &options = ExecuteScriptOptions()) override; 50*0b57cec5SDimitry Andric 51*0b57cec5SDimitry Andric Status 52*0b57cec5SDimitry Andric ExportFunctionDefinitionToInterpreter(StringList &function_def) override; 53*0b57cec5SDimitry Andric 54*0b57cec5SDimitry Andric bool GenerateTypeScriptFunction(StringList &input, std::string &output, 55*0b57cec5SDimitry Andric const void *name_token = nullptr) override; 56*0b57cec5SDimitry Andric 57*0b57cec5SDimitry Andric bool GenerateTypeSynthClass(StringList &input, std::string &output, 58*0b57cec5SDimitry Andric const void *name_token = nullptr) override; 59*0b57cec5SDimitry Andric 60*0b57cec5SDimitry Andric bool GenerateTypeSynthClass(const char *oneliner, std::string &output, 61*0b57cec5SDimitry Andric const void *name_token = nullptr) override; 62*0b57cec5SDimitry Andric 63*0b57cec5SDimitry Andric // use this if the function code is just a one-liner script 64*0b57cec5SDimitry Andric bool GenerateTypeScriptFunction(const char *oneliner, std::string &output, 65*0b57cec5SDimitry Andric const void *name_token = nullptr) override; 66*0b57cec5SDimitry Andric 67*0b57cec5SDimitry Andric bool GenerateScriptAliasFunction(StringList &input, 68*0b57cec5SDimitry Andric std::string &output) override; 69*0b57cec5SDimitry Andric 70*0b57cec5SDimitry Andric StructuredData::ObjectSP 71*0b57cec5SDimitry Andric CreateSyntheticScriptedProvider(const char *class_name, 72*0b57cec5SDimitry Andric lldb::ValueObjectSP valobj) override; 73*0b57cec5SDimitry Andric 74*0b57cec5SDimitry Andric StructuredData::GenericSP 75*0b57cec5SDimitry Andric CreateScriptCommandObject(const char *class_name) override; 76*0b57cec5SDimitry Andric 77*0b57cec5SDimitry Andric StructuredData::ObjectSP 78*0b57cec5SDimitry Andric CreateScriptedThreadPlan(const char *class_name, 799dba64beSDimitry Andric StructuredDataImpl *args_data, 809dba64beSDimitry Andric std::string &error_str, 81*0b57cec5SDimitry Andric lldb::ThreadPlanSP thread_plan) override; 82*0b57cec5SDimitry Andric 83*0b57cec5SDimitry Andric bool ScriptedThreadPlanExplainsStop(StructuredData::ObjectSP implementor_sp, 84*0b57cec5SDimitry Andric Event *event, 85*0b57cec5SDimitry Andric bool &script_error) override; 86*0b57cec5SDimitry Andric 87*0b57cec5SDimitry Andric bool ScriptedThreadPlanShouldStop(StructuredData::ObjectSP implementor_sp, 88*0b57cec5SDimitry Andric Event *event, bool &script_error) override; 89*0b57cec5SDimitry Andric 90*0b57cec5SDimitry Andric bool ScriptedThreadPlanIsStale(StructuredData::ObjectSP implementor_sp, 91*0b57cec5SDimitry Andric bool &script_error) override; 92*0b57cec5SDimitry Andric 93*0b57cec5SDimitry Andric lldb::StateType 94*0b57cec5SDimitry Andric ScriptedThreadPlanGetRunState(StructuredData::ObjectSP implementor_sp, 95*0b57cec5SDimitry Andric bool &script_error) override; 96*0b57cec5SDimitry Andric 97*0b57cec5SDimitry Andric StructuredData::GenericSP 98*0b57cec5SDimitry Andric CreateScriptedBreakpointResolver(const char *class_name, 99*0b57cec5SDimitry Andric StructuredDataImpl *args_data, 100*0b57cec5SDimitry Andric lldb::BreakpointSP &bkpt_sp) override; 101*0b57cec5SDimitry Andric bool ScriptedBreakpointResolverSearchCallback( 102*0b57cec5SDimitry Andric StructuredData::GenericSP implementor_sp, 103*0b57cec5SDimitry Andric SymbolContext *sym_ctx) override; 104*0b57cec5SDimitry Andric 105*0b57cec5SDimitry Andric lldb::SearchDepth ScriptedBreakpointResolverSearchDepth( 106*0b57cec5SDimitry Andric StructuredData::GenericSP implementor_sp) override; 107*0b57cec5SDimitry Andric 108*0b57cec5SDimitry Andric StructuredData::GenericSP 109*0b57cec5SDimitry Andric CreateFrameRecognizer(const char *class_name) override; 110*0b57cec5SDimitry Andric 111*0b57cec5SDimitry Andric lldb::ValueObjectListSP 112*0b57cec5SDimitry Andric GetRecognizedArguments(const StructuredData::ObjectSP &implementor, 113*0b57cec5SDimitry Andric lldb::StackFrameSP frame_sp) override; 114*0b57cec5SDimitry Andric 115*0b57cec5SDimitry Andric StructuredData::GenericSP 116*0b57cec5SDimitry Andric OSPlugin_CreatePluginObject(const char *class_name, 117*0b57cec5SDimitry Andric lldb::ProcessSP process_sp) override; 118*0b57cec5SDimitry Andric 119*0b57cec5SDimitry Andric StructuredData::DictionarySP 120*0b57cec5SDimitry Andric OSPlugin_RegisterInfo(StructuredData::ObjectSP os_plugin_object_sp) override; 121*0b57cec5SDimitry Andric 122*0b57cec5SDimitry Andric StructuredData::ArraySP 123*0b57cec5SDimitry Andric OSPlugin_ThreadsInfo(StructuredData::ObjectSP os_plugin_object_sp) override; 124*0b57cec5SDimitry Andric 125*0b57cec5SDimitry Andric StructuredData::StringSP 126*0b57cec5SDimitry Andric OSPlugin_RegisterContextData(StructuredData::ObjectSP os_plugin_object_sp, 127*0b57cec5SDimitry Andric lldb::tid_t thread_id) override; 128*0b57cec5SDimitry Andric 129*0b57cec5SDimitry Andric StructuredData::DictionarySP 130*0b57cec5SDimitry Andric OSPlugin_CreateThread(StructuredData::ObjectSP os_plugin_object_sp, 131*0b57cec5SDimitry Andric lldb::tid_t tid, lldb::addr_t context) override; 132*0b57cec5SDimitry Andric 133*0b57cec5SDimitry Andric StructuredData::ObjectSP 134*0b57cec5SDimitry Andric LoadPluginModule(const FileSpec &file_spec, 135*0b57cec5SDimitry Andric lldb_private::Status &error) override; 136*0b57cec5SDimitry Andric 137*0b57cec5SDimitry Andric StructuredData::DictionarySP 138*0b57cec5SDimitry Andric GetDynamicSettings(StructuredData::ObjectSP plugin_module_sp, Target *target, 139*0b57cec5SDimitry Andric const char *setting_name, 140*0b57cec5SDimitry Andric lldb_private::Status &error) override; 141*0b57cec5SDimitry Andric 142*0b57cec5SDimitry Andric size_t CalculateNumChildren(const StructuredData::ObjectSP &implementor, 143*0b57cec5SDimitry Andric uint32_t max) override; 144*0b57cec5SDimitry Andric 145*0b57cec5SDimitry Andric lldb::ValueObjectSP 146*0b57cec5SDimitry Andric GetChildAtIndex(const StructuredData::ObjectSP &implementor, 147*0b57cec5SDimitry Andric uint32_t idx) override; 148*0b57cec5SDimitry Andric 149*0b57cec5SDimitry Andric int GetIndexOfChildWithName(const StructuredData::ObjectSP &implementor, 150*0b57cec5SDimitry Andric const char *child_name) override; 151*0b57cec5SDimitry Andric 152*0b57cec5SDimitry Andric bool UpdateSynthProviderInstance( 153*0b57cec5SDimitry Andric const StructuredData::ObjectSP &implementor) override; 154*0b57cec5SDimitry Andric 155*0b57cec5SDimitry Andric bool MightHaveChildrenSynthProviderInstance( 156*0b57cec5SDimitry Andric const StructuredData::ObjectSP &implementor) override; 157*0b57cec5SDimitry Andric 158*0b57cec5SDimitry Andric lldb::ValueObjectSP 159*0b57cec5SDimitry Andric GetSyntheticValue(const StructuredData::ObjectSP &implementor) override; 160*0b57cec5SDimitry Andric 161*0b57cec5SDimitry Andric ConstString 162*0b57cec5SDimitry Andric GetSyntheticTypeName(const StructuredData::ObjectSP &implementor) override; 163*0b57cec5SDimitry Andric 164*0b57cec5SDimitry Andric bool 165*0b57cec5SDimitry Andric RunScriptBasedCommand(const char *impl_function, llvm::StringRef args, 166*0b57cec5SDimitry Andric ScriptedCommandSynchronicity synchronicity, 167*0b57cec5SDimitry Andric lldb_private::CommandReturnObject &cmd_retobj, 168*0b57cec5SDimitry Andric Status &error, 169*0b57cec5SDimitry Andric const lldb_private::ExecutionContext &exe_ctx) override; 170*0b57cec5SDimitry Andric 171*0b57cec5SDimitry Andric bool RunScriptBasedCommand( 172*0b57cec5SDimitry Andric StructuredData::GenericSP impl_obj_sp, llvm::StringRef args, 173*0b57cec5SDimitry Andric ScriptedCommandSynchronicity synchronicity, 174*0b57cec5SDimitry Andric lldb_private::CommandReturnObject &cmd_retobj, Status &error, 175*0b57cec5SDimitry Andric const lldb_private::ExecutionContext &exe_ctx) override; 176*0b57cec5SDimitry Andric 177*0b57cec5SDimitry Andric Status GenerateFunction(const char *signature, 178*0b57cec5SDimitry Andric const StringList &input) override; 179*0b57cec5SDimitry Andric 180480093f4SDimitry Andric Status GenerateBreakpointCommandCallbackData( 181480093f4SDimitry Andric StringList &input, 182480093f4SDimitry Andric std::string &output, 183480093f4SDimitry Andric bool has_extra_args) override; 184*0b57cec5SDimitry Andric 185*0b57cec5SDimitry Andric bool GenerateWatchpointCommandCallbackData(StringList &input, 186*0b57cec5SDimitry Andric std::string &output) override; 187*0b57cec5SDimitry Andric 188*0b57cec5SDimitry Andric bool GetScriptedSummary(const char *function_name, lldb::ValueObjectSP valobj, 189*0b57cec5SDimitry Andric StructuredData::ObjectSP &callee_wrapper_sp, 190*0b57cec5SDimitry Andric const TypeSummaryOptions &options, 191*0b57cec5SDimitry Andric std::string &retval) override; 192*0b57cec5SDimitry Andric 193*0b57cec5SDimitry Andric bool GetDocumentationForItem(const char *item, std::string &dest) override; 194*0b57cec5SDimitry Andric 195*0b57cec5SDimitry Andric bool GetShortHelpForCommandObject(StructuredData::GenericSP cmd_obj_sp, 196*0b57cec5SDimitry Andric std::string &dest) override; 197*0b57cec5SDimitry Andric 198*0b57cec5SDimitry Andric uint32_t 199*0b57cec5SDimitry Andric GetFlagsForCommandObject(StructuredData::GenericSP cmd_obj_sp) override; 200*0b57cec5SDimitry Andric 201*0b57cec5SDimitry Andric bool GetLongHelpForCommandObject(StructuredData::GenericSP cmd_obj_sp, 202*0b57cec5SDimitry Andric std::string &dest) override; 203*0b57cec5SDimitry Andric 204*0b57cec5SDimitry Andric bool CheckObjectExists(const char *name) override { 205*0b57cec5SDimitry Andric if (!name || !name[0]) 206*0b57cec5SDimitry Andric return false; 207*0b57cec5SDimitry Andric std::string temp; 208*0b57cec5SDimitry Andric return GetDocumentationForItem(name, temp); 209*0b57cec5SDimitry Andric } 210*0b57cec5SDimitry Andric 211*0b57cec5SDimitry Andric bool RunScriptFormatKeyword(const char *impl_function, Process *process, 212*0b57cec5SDimitry Andric std::string &output, Status &error) override; 213*0b57cec5SDimitry Andric 214*0b57cec5SDimitry Andric bool RunScriptFormatKeyword(const char *impl_function, Thread *thread, 215*0b57cec5SDimitry Andric std::string &output, Status &error) override; 216*0b57cec5SDimitry Andric 217*0b57cec5SDimitry Andric bool RunScriptFormatKeyword(const char *impl_function, Target *target, 218*0b57cec5SDimitry Andric std::string &output, Status &error) override; 219*0b57cec5SDimitry Andric 220*0b57cec5SDimitry Andric bool RunScriptFormatKeyword(const char *impl_function, StackFrame *frame, 221*0b57cec5SDimitry Andric std::string &output, Status &error) override; 222*0b57cec5SDimitry Andric 223*0b57cec5SDimitry Andric bool RunScriptFormatKeyword(const char *impl_function, ValueObject *value, 224*0b57cec5SDimitry Andric std::string &output, Status &error) override; 225*0b57cec5SDimitry Andric 226*0b57cec5SDimitry Andric bool 227480093f4SDimitry Andric LoadScriptingModule(const char *filename, bool init_session, 228*0b57cec5SDimitry Andric lldb_private::Status &error, 229*0b57cec5SDimitry Andric StructuredData::ObjectSP *module_sp = nullptr) override; 230*0b57cec5SDimitry Andric 231*0b57cec5SDimitry Andric bool IsReservedWord(const char *word) override; 232*0b57cec5SDimitry Andric 233*0b57cec5SDimitry Andric std::unique_ptr<ScriptInterpreterLocker> AcquireInterpreterLock() override; 234*0b57cec5SDimitry Andric 235*0b57cec5SDimitry Andric void CollectDataForBreakpointCommandCallback( 236*0b57cec5SDimitry Andric std::vector<BreakpointOptions *> &bp_options_vec, 237*0b57cec5SDimitry Andric CommandReturnObject &result) override; 238*0b57cec5SDimitry Andric 239*0b57cec5SDimitry Andric void 240*0b57cec5SDimitry Andric CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options, 241*0b57cec5SDimitry Andric CommandReturnObject &result) override; 242*0b57cec5SDimitry Andric 243*0b57cec5SDimitry Andric /// Set the callback body text into the callback for the breakpoint. 244*0b57cec5SDimitry Andric Status SetBreakpointCommandCallback(BreakpointOptions *bp_options, 245*0b57cec5SDimitry Andric const char *callback_body) override; 246*0b57cec5SDimitry Andric 247480093f4SDimitry Andric Status SetBreakpointCommandCallbackFunction( 248480093f4SDimitry Andric BreakpointOptions *bp_options, 249480093f4SDimitry Andric const char *function_name, 250480093f4SDimitry Andric StructuredData::ObjectSP extra_args_sp) override; 251*0b57cec5SDimitry Andric 252*0b57cec5SDimitry Andric /// This one is for deserialization: 253*0b57cec5SDimitry Andric Status SetBreakpointCommandCallback( 254*0b57cec5SDimitry Andric BreakpointOptions *bp_options, 255*0b57cec5SDimitry Andric std::unique_ptr<BreakpointOptions::CommandData> &data_up) override; 256*0b57cec5SDimitry 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 262*0b57cec5SDimitry Andric /// Set a one-liner as the callback for the watchpoint. 263*0b57cec5SDimitry Andric void SetWatchpointCommandCallback(WatchpointOptions *wp_options, 264*0b57cec5SDimitry Andric const char *oneliner) override; 265*0b57cec5SDimitry Andric 266*0b57cec5SDimitry Andric const char *GetDictionaryName() { return m_dictionary_name.c_str(); } 267*0b57cec5SDimitry Andric 268*0b57cec5SDimitry Andric PyThreadState *GetThreadState() { return m_command_thread_state; } 269*0b57cec5SDimitry Andric 270*0b57cec5SDimitry Andric void SetThreadState(PyThreadState *s) { 271*0b57cec5SDimitry Andric if (s) 272*0b57cec5SDimitry Andric m_command_thread_state = s; 273*0b57cec5SDimitry Andric } 274*0b57cec5SDimitry Andric 275*0b57cec5SDimitry Andric // IOHandlerDelegate 276*0b57cec5SDimitry Andric void IOHandlerActivated(IOHandler &io_handler, bool interactive) override; 277*0b57cec5SDimitry Andric 278*0b57cec5SDimitry Andric void IOHandlerInputComplete(IOHandler &io_handler, 279*0b57cec5SDimitry Andric std::string &data) override; 280*0b57cec5SDimitry Andric 281*0b57cec5SDimitry Andric static lldb::ScriptInterpreterSP CreateInstance(Debugger &debugger); 282*0b57cec5SDimitry Andric 283*0b57cec5SDimitry Andric // PluginInterface protocol 284*0b57cec5SDimitry Andric lldb_private::ConstString GetPluginName() override; 285*0b57cec5SDimitry Andric 286*0b57cec5SDimitry Andric uint32_t GetPluginVersion() override; 287*0b57cec5SDimitry Andric 288*0b57cec5SDimitry Andric class Locker : public ScriptInterpreterLocker { 289*0b57cec5SDimitry Andric public: 290*0b57cec5SDimitry Andric enum OnEntry { 291*0b57cec5SDimitry Andric AcquireLock = 0x0001, 292*0b57cec5SDimitry Andric InitSession = 0x0002, 293*0b57cec5SDimitry Andric InitGlobals = 0x0004, 294*0b57cec5SDimitry Andric NoSTDIN = 0x0008 295*0b57cec5SDimitry Andric }; 296*0b57cec5SDimitry Andric 297*0b57cec5SDimitry Andric enum OnLeave { 298*0b57cec5SDimitry Andric FreeLock = 0x0001, 299*0b57cec5SDimitry Andric FreeAcquiredLock = 0x0002, // do not free the lock if we already held it 300*0b57cec5SDimitry Andric // when calling constructor 301*0b57cec5SDimitry Andric TearDownSession = 0x0004 302*0b57cec5SDimitry Andric }; 303*0b57cec5SDimitry Andric 3049dba64beSDimitry Andric Locker(ScriptInterpreterPythonImpl *py_interpreter, 305*0b57cec5SDimitry 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); 309*0b57cec5SDimitry Andric 310*0b57cec5SDimitry Andric ~Locker() override; 311*0b57cec5SDimitry Andric 312*0b57cec5SDimitry Andric private: 313*0b57cec5SDimitry Andric bool DoAcquireLock(); 314*0b57cec5SDimitry Andric 3159dba64beSDimitry Andric bool DoInitSession(uint16_t on_entry_flags, lldb::FileSP in, 3169dba64beSDimitry Andric lldb::FileSP out, lldb::FileSP err); 317*0b57cec5SDimitry Andric 318*0b57cec5SDimitry Andric bool DoFreeLock(); 319*0b57cec5SDimitry Andric 320*0b57cec5SDimitry Andric bool DoTearDownSession(); 321*0b57cec5SDimitry Andric 322*0b57cec5SDimitry Andric bool m_teardown_session; 323*0b57cec5SDimitry Andric ScriptInterpreterPythonImpl *m_python_interpreter; 324*0b57cec5SDimitry Andric PyGILState_STATE m_GILState; 325*0b57cec5SDimitry Andric }; 326*0b57cec5SDimitry Andric 327*0b57cec5SDimitry Andric static bool BreakpointCallbackFunction(void *baton, 328*0b57cec5SDimitry Andric StoppointCallbackContext *context, 329*0b57cec5SDimitry Andric lldb::user_id_t break_id, 330*0b57cec5SDimitry Andric lldb::user_id_t break_loc_id); 331*0b57cec5SDimitry Andric static bool WatchpointCallbackFunction(void *baton, 332*0b57cec5SDimitry Andric StoppointCallbackContext *context, 333*0b57cec5SDimitry Andric lldb::user_id_t watch_id); 334*0b57cec5SDimitry Andric static void InitializePrivate(); 335*0b57cec5SDimitry Andric 336*0b57cec5SDimitry Andric class SynchronicityHandler { 337*0b57cec5SDimitry Andric private: 338*0b57cec5SDimitry Andric lldb::DebuggerSP m_debugger_sp; 339*0b57cec5SDimitry Andric ScriptedCommandSynchronicity m_synch_wanted; 340*0b57cec5SDimitry Andric bool m_old_asynch; 341*0b57cec5SDimitry Andric 342*0b57cec5SDimitry Andric public: 343*0b57cec5SDimitry Andric SynchronicityHandler(lldb::DebuggerSP, ScriptedCommandSynchronicity); 344*0b57cec5SDimitry Andric 345*0b57cec5SDimitry Andric ~SynchronicityHandler(); 346*0b57cec5SDimitry Andric }; 347*0b57cec5SDimitry Andric 348*0b57cec5SDimitry Andric enum class AddLocation { Beginning, End }; 349*0b57cec5SDimitry Andric 350*0b57cec5SDimitry Andric static void AddToSysPath(AddLocation location, std::string path); 351*0b57cec5SDimitry Andric 3529dba64beSDimitry Andric bool EnterSession(uint16_t on_entry_flags, lldb::FileSP in, lldb::FileSP out, 3539dba64beSDimitry Andric lldb::FileSP err); 354*0b57cec5SDimitry Andric 355*0b57cec5SDimitry Andric void LeaveSession(); 356*0b57cec5SDimitry Andric 357*0b57cec5SDimitry Andric uint32_t IsExecutingPython() const { return m_lock_count > 0; } 358*0b57cec5SDimitry Andric 359*0b57cec5SDimitry Andric uint32_t IncrementLockCount() { return ++m_lock_count; } 360*0b57cec5SDimitry Andric 361*0b57cec5SDimitry Andric uint32_t DecrementLockCount() { 362*0b57cec5SDimitry Andric if (m_lock_count > 0) 363*0b57cec5SDimitry Andric --m_lock_count; 364*0b57cec5SDimitry Andric return m_lock_count; 365*0b57cec5SDimitry Andric } 366*0b57cec5SDimitry Andric 367*0b57cec5SDimitry Andric enum ActiveIOHandler { 368*0b57cec5SDimitry Andric eIOHandlerNone, 369*0b57cec5SDimitry Andric eIOHandlerBreakpoint, 370*0b57cec5SDimitry Andric eIOHandlerWatchpoint 371*0b57cec5SDimitry Andric }; 372*0b57cec5SDimitry Andric 3739dba64beSDimitry Andric python::PythonModule &GetMainModule(); 374*0b57cec5SDimitry Andric 3759dba64beSDimitry Andric python::PythonDictionary &GetSessionDictionary(); 376*0b57cec5SDimitry Andric 3779dba64beSDimitry Andric python::PythonDictionary &GetSysModuleDictionary(); 378*0b57cec5SDimitry Andric 379480093f4SDimitry Andric llvm::Expected<unsigned> GetMaxPositionalArgumentsForCallable( 380480093f4SDimitry Andric const llvm::StringRef &callable_name) override; 381480093f4SDimitry Andric 382*0b57cec5SDimitry Andric bool GetEmbeddedInterpreterModuleObjects(); 383*0b57cec5SDimitry Andric 3849dba64beSDimitry Andric bool SetStdHandle(lldb::FileSP file, const char *py_name, 3859dba64beSDimitry Andric python::PythonObject &save_file, const char *mode); 386*0b57cec5SDimitry 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; 395*0b57cec5SDimitry Andric std::string m_dictionary_name; 396*0b57cec5SDimitry Andric ActiveIOHandler m_active_io_handler; 397*0b57cec5SDimitry Andric bool m_session_is_active; 3985ffd83dbSDimitry Andric bool m_pty_secondary_is_open; 399*0b57cec5SDimitry Andric bool m_valid_session; 400*0b57cec5SDimitry Andric uint32_t m_lock_count; 401*0b57cec5SDimitry Andric PyThreadState *m_command_thread_state; 402*0b57cec5SDimitry Andric }; 403*0b57cec5SDimitry Andric 404*0b57cec5SDimitry Andric class IOHandlerPythonInterpreter : public IOHandler { 405*0b57cec5SDimitry Andric public: 406*0b57cec5SDimitry Andric IOHandlerPythonInterpreter(Debugger &debugger, 407*0b57cec5SDimitry Andric ScriptInterpreterPythonImpl *python) 408*0b57cec5SDimitry Andric : IOHandler(debugger, IOHandler::Type::PythonInterpreter), 409*0b57cec5SDimitry Andric m_python(python) {} 410*0b57cec5SDimitry Andric 411*0b57cec5SDimitry Andric ~IOHandlerPythonInterpreter() override {} 412*0b57cec5SDimitry Andric 413*0b57cec5SDimitry Andric ConstString GetControlSequence(char ch) override { 414*0b57cec5SDimitry Andric if (ch == 'd') 415*0b57cec5SDimitry Andric return ConstString("quit()\n"); 416*0b57cec5SDimitry Andric return ConstString(); 417*0b57cec5SDimitry Andric } 418*0b57cec5SDimitry Andric 419*0b57cec5SDimitry Andric void Run() override { 420*0b57cec5SDimitry Andric if (m_python) { 421*0b57cec5SDimitry Andric int stdin_fd = GetInputFD(); 422*0b57cec5SDimitry Andric if (stdin_fd >= 0) { 423*0b57cec5SDimitry Andric Terminal terminal(stdin_fd); 424*0b57cec5SDimitry Andric TerminalState terminal_state; 425*0b57cec5SDimitry Andric const bool is_a_tty = terminal.IsATerminal(); 426*0b57cec5SDimitry Andric 427*0b57cec5SDimitry Andric if (is_a_tty) { 428*0b57cec5SDimitry Andric terminal_state.Save(stdin_fd, false); 429*0b57cec5SDimitry Andric terminal.SetCanonical(false); 430*0b57cec5SDimitry Andric terminal.SetEcho(true); 431*0b57cec5SDimitry Andric } 432*0b57cec5SDimitry Andric 433*0b57cec5SDimitry Andric ScriptInterpreterPythonImpl::Locker locker( 434*0b57cec5SDimitry Andric m_python, 435*0b57cec5SDimitry Andric ScriptInterpreterPythonImpl::Locker::AcquireLock | 436*0b57cec5SDimitry Andric ScriptInterpreterPythonImpl::Locker::InitSession | 437*0b57cec5SDimitry Andric ScriptInterpreterPythonImpl::Locker::InitGlobals, 438*0b57cec5SDimitry Andric ScriptInterpreterPythonImpl::Locker::FreeAcquiredLock | 439*0b57cec5SDimitry Andric ScriptInterpreterPythonImpl::Locker::TearDownSession); 440*0b57cec5SDimitry Andric 441*0b57cec5SDimitry Andric // The following call drops into the embedded interpreter loop and 442*0b57cec5SDimitry Andric // stays there until the user chooses to exit from the Python 443*0b57cec5SDimitry Andric // interpreter. This embedded interpreter will, as any Python code that 444*0b57cec5SDimitry Andric // performs I/O, unlock the GIL before a system call that can hang, and 445*0b57cec5SDimitry Andric // lock it when the syscall has returned. 446*0b57cec5SDimitry Andric 447*0b57cec5SDimitry Andric // We need to surround the call to the embedded interpreter with calls 448*0b57cec5SDimitry Andric // to PyGILState_Ensure and PyGILState_Release (using the Locker 449*0b57cec5SDimitry Andric // above). This is because Python has a global lock which must be held 450*0b57cec5SDimitry Andric // whenever we want to touch any Python objects. Otherwise, if the user 451*0b57cec5SDimitry Andric // calls Python code, the interpreter state will be off, and things 452*0b57cec5SDimitry Andric // could hang (it's happened before). 453*0b57cec5SDimitry Andric 454*0b57cec5SDimitry Andric StreamString run_string; 455*0b57cec5SDimitry Andric run_string.Printf("run_python_interpreter (%s)", 456*0b57cec5SDimitry Andric m_python->GetDictionaryName()); 457*0b57cec5SDimitry Andric PyRun_SimpleString(run_string.GetData()); 458*0b57cec5SDimitry Andric 459*0b57cec5SDimitry Andric if (is_a_tty) 460*0b57cec5SDimitry Andric terminal_state.Restore(); 461*0b57cec5SDimitry Andric } 462*0b57cec5SDimitry Andric } 463*0b57cec5SDimitry Andric SetIsDone(true); 464*0b57cec5SDimitry Andric } 465*0b57cec5SDimitry Andric 466*0b57cec5SDimitry Andric void Cancel() override {} 467*0b57cec5SDimitry Andric 468*0b57cec5SDimitry Andric bool Interrupt() override { return m_python->Interrupt(); } 469*0b57cec5SDimitry Andric 470*0b57cec5SDimitry Andric void GotEOF() override {} 471*0b57cec5SDimitry Andric 472*0b57cec5SDimitry Andric protected: 473*0b57cec5SDimitry Andric ScriptInterpreterPythonImpl *m_python; 474*0b57cec5SDimitry Andric }; 475*0b57cec5SDimitry Andric 476*0b57cec5SDimitry Andric } // namespace lldb_private 477*0b57cec5SDimitry Andric 478*0b57cec5SDimitry Andric #endif 479