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 9*0b57cec5SDimitry Andric #ifdef LLDB_DISABLE_PYTHON 10*0b57cec5SDimitry Andric 11*0b57cec5SDimitry Andric // Python is disabled in this build 12*0b57cec5SDimitry Andric 13*0b57cec5SDimitry Andric #else 14*0b57cec5SDimitry Andric 15*0b57cec5SDimitry Andric #include "lldb-python.h" 16*0b57cec5SDimitry Andric 17*0b57cec5SDimitry Andric #include "PythonDataObjects.h" 18*0b57cec5SDimitry Andric #include "ScriptInterpreterPython.h" 19*0b57cec5SDimitry Andric 20*0b57cec5SDimitry Andric #include "lldb/Host/Terminal.h" 21*0b57cec5SDimitry Andric #include "lldb/Utility/StreamString.h" 22*0b57cec5SDimitry Andric 23*0b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 24*0b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 25*0b57cec5SDimitry Andric 26*0b57cec5SDimitry Andric namespace lldb_private { 27*0b57cec5SDimitry Andric class IOHandlerPythonInterpreter; 28*0b57cec5SDimitry Andric class ScriptInterpreterPythonImpl : public ScriptInterpreterPython { 29*0b57cec5SDimitry Andric public: 30*0b57cec5SDimitry Andric friend class IOHandlerPythonInterpreter; 31*0b57cec5SDimitry Andric 32*0b57cec5SDimitry Andric ScriptInterpreterPythonImpl(Debugger &debugger); 33*0b57cec5SDimitry Andric 34*0b57cec5SDimitry Andric ~ScriptInterpreterPythonImpl() override; 35*0b57cec5SDimitry Andric 36*0b57cec5SDimitry Andric bool Interrupt() override; 37*0b57cec5SDimitry Andric 38*0b57cec5SDimitry Andric bool ExecuteOneLine( 39*0b57cec5SDimitry Andric llvm::StringRef command, CommandReturnObject *result, 40*0b57cec5SDimitry Andric const ExecuteScriptOptions &options = ExecuteScriptOptions()) override; 41*0b57cec5SDimitry Andric 42*0b57cec5SDimitry Andric void ExecuteInterpreterLoop() override; 43*0b57cec5SDimitry Andric 44*0b57cec5SDimitry Andric bool ExecuteOneLineWithReturn( 45*0b57cec5SDimitry Andric llvm::StringRef in_string, 46*0b57cec5SDimitry Andric ScriptInterpreter::ScriptReturnType return_type, void *ret_value, 47*0b57cec5SDimitry Andric const ExecuteScriptOptions &options = ExecuteScriptOptions()) override; 48*0b57cec5SDimitry Andric 49*0b57cec5SDimitry Andric lldb_private::Status ExecuteMultipleLines( 50*0b57cec5SDimitry Andric const char *in_string, 51*0b57cec5SDimitry Andric const ExecuteScriptOptions &options = ExecuteScriptOptions()) override; 52*0b57cec5SDimitry Andric 53*0b57cec5SDimitry Andric Status 54*0b57cec5SDimitry Andric ExportFunctionDefinitionToInterpreter(StringList &function_def) override; 55*0b57cec5SDimitry Andric 56*0b57cec5SDimitry Andric bool GenerateTypeScriptFunction(StringList &input, std::string &output, 57*0b57cec5SDimitry Andric const void *name_token = nullptr) override; 58*0b57cec5SDimitry Andric 59*0b57cec5SDimitry Andric bool GenerateTypeSynthClass(StringList &input, std::string &output, 60*0b57cec5SDimitry Andric const void *name_token = nullptr) override; 61*0b57cec5SDimitry Andric 62*0b57cec5SDimitry Andric bool GenerateTypeSynthClass(const char *oneliner, std::string &output, 63*0b57cec5SDimitry Andric const void *name_token = nullptr) override; 64*0b57cec5SDimitry Andric 65*0b57cec5SDimitry Andric // use this if the function code is just a one-liner script 66*0b57cec5SDimitry Andric bool GenerateTypeScriptFunction(const char *oneliner, std::string &output, 67*0b57cec5SDimitry Andric const void *name_token = nullptr) override; 68*0b57cec5SDimitry Andric 69*0b57cec5SDimitry Andric bool GenerateScriptAliasFunction(StringList &input, 70*0b57cec5SDimitry Andric std::string &output) override; 71*0b57cec5SDimitry Andric 72*0b57cec5SDimitry Andric StructuredData::ObjectSP 73*0b57cec5SDimitry Andric CreateSyntheticScriptedProvider(const char *class_name, 74*0b57cec5SDimitry Andric lldb::ValueObjectSP valobj) override; 75*0b57cec5SDimitry Andric 76*0b57cec5SDimitry Andric StructuredData::GenericSP 77*0b57cec5SDimitry Andric CreateScriptCommandObject(const char *class_name) override; 78*0b57cec5SDimitry Andric 79*0b57cec5SDimitry Andric StructuredData::ObjectSP 80*0b57cec5SDimitry Andric CreateScriptedThreadPlan(const char *class_name, 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 180*0b57cec5SDimitry Andric Status GenerateBreakpointCommandCallbackData(StringList &input, 181*0b57cec5SDimitry Andric std::string &output) override; 182*0b57cec5SDimitry Andric 183*0b57cec5SDimitry Andric bool GenerateWatchpointCommandCallbackData(StringList &input, 184*0b57cec5SDimitry Andric std::string &output) override; 185*0b57cec5SDimitry Andric 186*0b57cec5SDimitry Andric bool GetScriptedSummary(const char *function_name, lldb::ValueObjectSP valobj, 187*0b57cec5SDimitry Andric StructuredData::ObjectSP &callee_wrapper_sp, 188*0b57cec5SDimitry Andric const TypeSummaryOptions &options, 189*0b57cec5SDimitry Andric std::string &retval) override; 190*0b57cec5SDimitry Andric 191*0b57cec5SDimitry Andric void Clear() 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 227*0b57cec5SDimitry Andric LoadScriptingModule(const char *filename, bool can_reload, 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 247*0b57cec5SDimitry Andric void SetBreakpointCommandCallbackFunction(BreakpointOptions *bp_options, 248*0b57cec5SDimitry Andric const char *function_name) override; 249*0b57cec5SDimitry Andric 250*0b57cec5SDimitry Andric /// This one is for deserialization: 251*0b57cec5SDimitry Andric Status SetBreakpointCommandCallback( 252*0b57cec5SDimitry Andric BreakpointOptions *bp_options, 253*0b57cec5SDimitry Andric std::unique_ptr<BreakpointOptions::CommandData> &data_up) override; 254*0b57cec5SDimitry Andric 255*0b57cec5SDimitry Andric /// Set a one-liner as the callback for the watchpoint. 256*0b57cec5SDimitry Andric void SetWatchpointCommandCallback(WatchpointOptions *wp_options, 257*0b57cec5SDimitry Andric const char *oneliner) override; 258*0b57cec5SDimitry Andric 259*0b57cec5SDimitry Andric void ResetOutputFileHandle(FILE *new_fh) override; 260*0b57cec5SDimitry Andric 261*0b57cec5SDimitry Andric const char *GetDictionaryName() { return m_dictionary_name.c_str(); } 262*0b57cec5SDimitry Andric 263*0b57cec5SDimitry Andric PyThreadState *GetThreadState() { return m_command_thread_state; } 264*0b57cec5SDimitry Andric 265*0b57cec5SDimitry Andric void SetThreadState(PyThreadState *s) { 266*0b57cec5SDimitry Andric if (s) 267*0b57cec5SDimitry Andric m_command_thread_state = s; 268*0b57cec5SDimitry Andric } 269*0b57cec5SDimitry Andric 270*0b57cec5SDimitry Andric // IOHandlerDelegate 271*0b57cec5SDimitry Andric void IOHandlerActivated(IOHandler &io_handler, bool interactive) override; 272*0b57cec5SDimitry Andric 273*0b57cec5SDimitry Andric void IOHandlerInputComplete(IOHandler &io_handler, 274*0b57cec5SDimitry Andric std::string &data) override; 275*0b57cec5SDimitry Andric 276*0b57cec5SDimitry Andric static lldb::ScriptInterpreterSP CreateInstance(Debugger &debugger); 277*0b57cec5SDimitry Andric 278*0b57cec5SDimitry Andric // PluginInterface protocol 279*0b57cec5SDimitry Andric lldb_private::ConstString GetPluginName() override; 280*0b57cec5SDimitry Andric 281*0b57cec5SDimitry Andric uint32_t GetPluginVersion() override; 282*0b57cec5SDimitry Andric 283*0b57cec5SDimitry Andric class Locker : public ScriptInterpreterLocker { 284*0b57cec5SDimitry Andric public: 285*0b57cec5SDimitry Andric enum OnEntry { 286*0b57cec5SDimitry Andric AcquireLock = 0x0001, 287*0b57cec5SDimitry Andric InitSession = 0x0002, 288*0b57cec5SDimitry Andric InitGlobals = 0x0004, 289*0b57cec5SDimitry Andric NoSTDIN = 0x0008 290*0b57cec5SDimitry Andric }; 291*0b57cec5SDimitry Andric 292*0b57cec5SDimitry Andric enum OnLeave { 293*0b57cec5SDimitry Andric FreeLock = 0x0001, 294*0b57cec5SDimitry Andric FreeAcquiredLock = 0x0002, // do not free the lock if we already held it 295*0b57cec5SDimitry Andric // when calling constructor 296*0b57cec5SDimitry Andric TearDownSession = 0x0004 297*0b57cec5SDimitry Andric }; 298*0b57cec5SDimitry Andric 299*0b57cec5SDimitry Andric Locker(ScriptInterpreterPythonImpl *py_interpreter = nullptr, 300*0b57cec5SDimitry Andric uint16_t on_entry = AcquireLock | InitSession, 301*0b57cec5SDimitry Andric uint16_t on_leave = FreeLock | TearDownSession, FILE *in = nullptr, 302*0b57cec5SDimitry Andric FILE *out = nullptr, FILE *err = nullptr); 303*0b57cec5SDimitry Andric 304*0b57cec5SDimitry Andric ~Locker() override; 305*0b57cec5SDimitry Andric 306*0b57cec5SDimitry Andric private: 307*0b57cec5SDimitry Andric bool DoAcquireLock(); 308*0b57cec5SDimitry Andric 309*0b57cec5SDimitry Andric bool DoInitSession(uint16_t on_entry_flags, FILE *in, FILE *out, FILE *err); 310*0b57cec5SDimitry Andric 311*0b57cec5SDimitry Andric bool DoFreeLock(); 312*0b57cec5SDimitry Andric 313*0b57cec5SDimitry Andric bool DoTearDownSession(); 314*0b57cec5SDimitry Andric 315*0b57cec5SDimitry Andric bool m_teardown_session; 316*0b57cec5SDimitry Andric ScriptInterpreterPythonImpl *m_python_interpreter; 317*0b57cec5SDimitry Andric // FILE* m_tmp_fh; 318*0b57cec5SDimitry Andric PyGILState_STATE m_GILState; 319*0b57cec5SDimitry Andric }; 320*0b57cec5SDimitry Andric 321*0b57cec5SDimitry Andric static bool BreakpointCallbackFunction(void *baton, 322*0b57cec5SDimitry Andric StoppointCallbackContext *context, 323*0b57cec5SDimitry Andric lldb::user_id_t break_id, 324*0b57cec5SDimitry Andric lldb::user_id_t break_loc_id); 325*0b57cec5SDimitry Andric static bool WatchpointCallbackFunction(void *baton, 326*0b57cec5SDimitry Andric StoppointCallbackContext *context, 327*0b57cec5SDimitry Andric lldb::user_id_t watch_id); 328*0b57cec5SDimitry Andric static void InitializePrivate(); 329*0b57cec5SDimitry Andric 330*0b57cec5SDimitry Andric class SynchronicityHandler { 331*0b57cec5SDimitry Andric private: 332*0b57cec5SDimitry Andric lldb::DebuggerSP m_debugger_sp; 333*0b57cec5SDimitry Andric ScriptedCommandSynchronicity m_synch_wanted; 334*0b57cec5SDimitry Andric bool m_old_asynch; 335*0b57cec5SDimitry Andric 336*0b57cec5SDimitry Andric public: 337*0b57cec5SDimitry Andric SynchronicityHandler(lldb::DebuggerSP, ScriptedCommandSynchronicity); 338*0b57cec5SDimitry Andric 339*0b57cec5SDimitry Andric ~SynchronicityHandler(); 340*0b57cec5SDimitry Andric }; 341*0b57cec5SDimitry Andric 342*0b57cec5SDimitry Andric enum class AddLocation { Beginning, End }; 343*0b57cec5SDimitry Andric 344*0b57cec5SDimitry Andric static void AddToSysPath(AddLocation location, std::string path); 345*0b57cec5SDimitry Andric 346*0b57cec5SDimitry Andric bool EnterSession(uint16_t on_entry_flags, FILE *in, FILE *out, FILE *err); 347*0b57cec5SDimitry Andric 348*0b57cec5SDimitry Andric void LeaveSession(); 349*0b57cec5SDimitry Andric 350*0b57cec5SDimitry Andric void SaveTerminalState(int fd); 351*0b57cec5SDimitry Andric 352*0b57cec5SDimitry Andric void RestoreTerminalState(); 353*0b57cec5SDimitry Andric 354*0b57cec5SDimitry Andric uint32_t IsExecutingPython() const { return m_lock_count > 0; } 355*0b57cec5SDimitry Andric 356*0b57cec5SDimitry Andric uint32_t IncrementLockCount() { return ++m_lock_count; } 357*0b57cec5SDimitry Andric 358*0b57cec5SDimitry Andric uint32_t DecrementLockCount() { 359*0b57cec5SDimitry Andric if (m_lock_count > 0) 360*0b57cec5SDimitry Andric --m_lock_count; 361*0b57cec5SDimitry Andric return m_lock_count; 362*0b57cec5SDimitry Andric } 363*0b57cec5SDimitry Andric 364*0b57cec5SDimitry Andric enum ActiveIOHandler { 365*0b57cec5SDimitry Andric eIOHandlerNone, 366*0b57cec5SDimitry Andric eIOHandlerBreakpoint, 367*0b57cec5SDimitry Andric eIOHandlerWatchpoint 368*0b57cec5SDimitry Andric }; 369*0b57cec5SDimitry Andric 370*0b57cec5SDimitry Andric PythonObject &GetMainModule(); 371*0b57cec5SDimitry Andric 372*0b57cec5SDimitry Andric PythonDictionary &GetSessionDictionary(); 373*0b57cec5SDimitry Andric 374*0b57cec5SDimitry Andric PythonDictionary &GetSysModuleDictionary(); 375*0b57cec5SDimitry Andric 376*0b57cec5SDimitry Andric bool GetEmbeddedInterpreterModuleObjects(); 377*0b57cec5SDimitry Andric 378*0b57cec5SDimitry Andric bool SetStdHandle(File &file, const char *py_name, PythonFile &save_file, 379*0b57cec5SDimitry Andric const char *mode); 380*0b57cec5SDimitry Andric 381*0b57cec5SDimitry Andric PythonFile m_saved_stdin; 382*0b57cec5SDimitry Andric PythonFile m_saved_stdout; 383*0b57cec5SDimitry Andric PythonFile m_saved_stderr; 384*0b57cec5SDimitry Andric PythonObject m_main_module; 385*0b57cec5SDimitry Andric PythonDictionary m_session_dict; 386*0b57cec5SDimitry Andric PythonDictionary m_sys_module_dict; 387*0b57cec5SDimitry Andric PythonObject m_run_one_line_function; 388*0b57cec5SDimitry Andric PythonObject m_run_one_line_str_global; 389*0b57cec5SDimitry Andric std::string m_dictionary_name; 390*0b57cec5SDimitry Andric TerminalState m_terminal_state; 391*0b57cec5SDimitry Andric ActiveIOHandler m_active_io_handler; 392*0b57cec5SDimitry Andric bool m_session_is_active; 393*0b57cec5SDimitry Andric bool m_pty_slave_is_open; 394*0b57cec5SDimitry Andric bool m_valid_session; 395*0b57cec5SDimitry Andric uint32_t m_lock_count; 396*0b57cec5SDimitry Andric PyThreadState *m_command_thread_state; 397*0b57cec5SDimitry Andric }; 398*0b57cec5SDimitry Andric 399*0b57cec5SDimitry Andric class IOHandlerPythonInterpreter : public IOHandler { 400*0b57cec5SDimitry Andric public: 401*0b57cec5SDimitry Andric IOHandlerPythonInterpreter(Debugger &debugger, 402*0b57cec5SDimitry Andric ScriptInterpreterPythonImpl *python) 403*0b57cec5SDimitry Andric : IOHandler(debugger, IOHandler::Type::PythonInterpreter), 404*0b57cec5SDimitry Andric m_python(python) {} 405*0b57cec5SDimitry Andric 406*0b57cec5SDimitry Andric ~IOHandlerPythonInterpreter() override {} 407*0b57cec5SDimitry Andric 408*0b57cec5SDimitry Andric ConstString GetControlSequence(char ch) override { 409*0b57cec5SDimitry Andric if (ch == 'd') 410*0b57cec5SDimitry Andric return ConstString("quit()\n"); 411*0b57cec5SDimitry Andric return ConstString(); 412*0b57cec5SDimitry Andric } 413*0b57cec5SDimitry Andric 414*0b57cec5SDimitry Andric void Run() override { 415*0b57cec5SDimitry Andric if (m_python) { 416*0b57cec5SDimitry Andric int stdin_fd = GetInputFD(); 417*0b57cec5SDimitry Andric if (stdin_fd >= 0) { 418*0b57cec5SDimitry Andric Terminal terminal(stdin_fd); 419*0b57cec5SDimitry Andric TerminalState terminal_state; 420*0b57cec5SDimitry Andric const bool is_a_tty = terminal.IsATerminal(); 421*0b57cec5SDimitry Andric 422*0b57cec5SDimitry Andric if (is_a_tty) { 423*0b57cec5SDimitry Andric terminal_state.Save(stdin_fd, false); 424*0b57cec5SDimitry Andric terminal.SetCanonical(false); 425*0b57cec5SDimitry Andric terminal.SetEcho(true); 426*0b57cec5SDimitry Andric } 427*0b57cec5SDimitry Andric 428*0b57cec5SDimitry Andric ScriptInterpreterPythonImpl::Locker locker( 429*0b57cec5SDimitry Andric m_python, 430*0b57cec5SDimitry Andric ScriptInterpreterPythonImpl::Locker::AcquireLock | 431*0b57cec5SDimitry Andric ScriptInterpreterPythonImpl::Locker::InitSession | 432*0b57cec5SDimitry Andric ScriptInterpreterPythonImpl::Locker::InitGlobals, 433*0b57cec5SDimitry Andric ScriptInterpreterPythonImpl::Locker::FreeAcquiredLock | 434*0b57cec5SDimitry Andric ScriptInterpreterPythonImpl::Locker::TearDownSession); 435*0b57cec5SDimitry Andric 436*0b57cec5SDimitry Andric // The following call drops into the embedded interpreter loop and 437*0b57cec5SDimitry Andric // stays there until the user chooses to exit from the Python 438*0b57cec5SDimitry Andric // interpreter. This embedded interpreter will, as any Python code that 439*0b57cec5SDimitry Andric // performs I/O, unlock the GIL before a system call that can hang, and 440*0b57cec5SDimitry Andric // lock it when the syscall has returned. 441*0b57cec5SDimitry Andric 442*0b57cec5SDimitry Andric // We need to surround the call to the embedded interpreter with calls 443*0b57cec5SDimitry Andric // to PyGILState_Ensure and PyGILState_Release (using the Locker 444*0b57cec5SDimitry Andric // above). This is because Python has a global lock which must be held 445*0b57cec5SDimitry Andric // whenever we want to touch any Python objects. Otherwise, if the user 446*0b57cec5SDimitry Andric // calls Python code, the interpreter state will be off, and things 447*0b57cec5SDimitry Andric // could hang (it's happened before). 448*0b57cec5SDimitry Andric 449*0b57cec5SDimitry Andric StreamString run_string; 450*0b57cec5SDimitry Andric run_string.Printf("run_python_interpreter (%s)", 451*0b57cec5SDimitry Andric m_python->GetDictionaryName()); 452*0b57cec5SDimitry Andric PyRun_SimpleString(run_string.GetData()); 453*0b57cec5SDimitry Andric 454*0b57cec5SDimitry Andric if (is_a_tty) 455*0b57cec5SDimitry Andric terminal_state.Restore(); 456*0b57cec5SDimitry Andric } 457*0b57cec5SDimitry Andric } 458*0b57cec5SDimitry Andric SetIsDone(true); 459*0b57cec5SDimitry Andric } 460*0b57cec5SDimitry Andric 461*0b57cec5SDimitry Andric void Cancel() override {} 462*0b57cec5SDimitry Andric 463*0b57cec5SDimitry Andric bool Interrupt() override { return m_python->Interrupt(); } 464*0b57cec5SDimitry Andric 465*0b57cec5SDimitry Andric void GotEOF() override {} 466*0b57cec5SDimitry Andric 467*0b57cec5SDimitry Andric protected: 468*0b57cec5SDimitry Andric ScriptInterpreterPythonImpl *m_python; 469*0b57cec5SDimitry Andric }; 470*0b57cec5SDimitry Andric 471*0b57cec5SDimitry Andric } // namespace lldb_private 472*0b57cec5SDimitry Andric 473*0b57cec5SDimitry Andric #endif 474