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 109e8d8bef9SDimitry Andric CreateScriptedStopHook(lldb::TargetSP target_sp, const char *class_name, 110e8d8bef9SDimitry Andric StructuredDataImpl *args_data, Status &error) override; 111e8d8bef9SDimitry Andric 112e8d8bef9SDimitry Andric bool ScriptedStopHookHandleStop(StructuredData::GenericSP implementor_sp, 113e8d8bef9SDimitry Andric ExecutionContext &exc_ctx, 114e8d8bef9SDimitry Andric lldb::StreamSP stream_sp) override; 115e8d8bef9SDimitry Andric 116e8d8bef9SDimitry Andric StructuredData::GenericSP 117*0b57cec5SDimitry Andric CreateFrameRecognizer(const char *class_name) override; 118*0b57cec5SDimitry Andric 119*0b57cec5SDimitry Andric lldb::ValueObjectListSP 120*0b57cec5SDimitry Andric GetRecognizedArguments(const StructuredData::ObjectSP &implementor, 121*0b57cec5SDimitry Andric lldb::StackFrameSP frame_sp) override; 122*0b57cec5SDimitry Andric 123*0b57cec5SDimitry Andric StructuredData::GenericSP 124*0b57cec5SDimitry Andric OSPlugin_CreatePluginObject(const char *class_name, 125*0b57cec5SDimitry Andric lldb::ProcessSP process_sp) override; 126*0b57cec5SDimitry Andric 127*0b57cec5SDimitry Andric StructuredData::DictionarySP 128*0b57cec5SDimitry Andric OSPlugin_RegisterInfo(StructuredData::ObjectSP os_plugin_object_sp) override; 129*0b57cec5SDimitry Andric 130*0b57cec5SDimitry Andric StructuredData::ArraySP 131*0b57cec5SDimitry Andric OSPlugin_ThreadsInfo(StructuredData::ObjectSP os_plugin_object_sp) override; 132*0b57cec5SDimitry Andric 133*0b57cec5SDimitry Andric StructuredData::StringSP 134*0b57cec5SDimitry Andric OSPlugin_RegisterContextData(StructuredData::ObjectSP os_plugin_object_sp, 135*0b57cec5SDimitry Andric lldb::tid_t thread_id) override; 136*0b57cec5SDimitry Andric 137*0b57cec5SDimitry Andric StructuredData::DictionarySP 138*0b57cec5SDimitry Andric OSPlugin_CreateThread(StructuredData::ObjectSP os_plugin_object_sp, 139*0b57cec5SDimitry Andric lldb::tid_t tid, lldb::addr_t context) override; 140*0b57cec5SDimitry Andric 141*0b57cec5SDimitry Andric StructuredData::ObjectSP 142*0b57cec5SDimitry Andric LoadPluginModule(const FileSpec &file_spec, 143*0b57cec5SDimitry Andric lldb_private::Status &error) override; 144*0b57cec5SDimitry Andric 145*0b57cec5SDimitry Andric StructuredData::DictionarySP 146*0b57cec5SDimitry Andric GetDynamicSettings(StructuredData::ObjectSP plugin_module_sp, Target *target, 147*0b57cec5SDimitry Andric const char *setting_name, 148*0b57cec5SDimitry Andric lldb_private::Status &error) override; 149*0b57cec5SDimitry Andric 150*0b57cec5SDimitry Andric size_t CalculateNumChildren(const StructuredData::ObjectSP &implementor, 151*0b57cec5SDimitry Andric uint32_t max) override; 152*0b57cec5SDimitry Andric 153*0b57cec5SDimitry Andric lldb::ValueObjectSP 154*0b57cec5SDimitry Andric GetChildAtIndex(const StructuredData::ObjectSP &implementor, 155*0b57cec5SDimitry Andric uint32_t idx) override; 156*0b57cec5SDimitry Andric 157*0b57cec5SDimitry Andric int GetIndexOfChildWithName(const StructuredData::ObjectSP &implementor, 158*0b57cec5SDimitry Andric const char *child_name) override; 159*0b57cec5SDimitry Andric 160*0b57cec5SDimitry Andric bool UpdateSynthProviderInstance( 161*0b57cec5SDimitry Andric const StructuredData::ObjectSP &implementor) override; 162*0b57cec5SDimitry Andric 163*0b57cec5SDimitry Andric bool MightHaveChildrenSynthProviderInstance( 164*0b57cec5SDimitry Andric const StructuredData::ObjectSP &implementor) override; 165*0b57cec5SDimitry Andric 166*0b57cec5SDimitry Andric lldb::ValueObjectSP 167*0b57cec5SDimitry Andric GetSyntheticValue(const StructuredData::ObjectSP &implementor) override; 168*0b57cec5SDimitry Andric 169*0b57cec5SDimitry Andric ConstString 170*0b57cec5SDimitry Andric GetSyntheticTypeName(const StructuredData::ObjectSP &implementor) override; 171*0b57cec5SDimitry Andric 172*0b57cec5SDimitry Andric bool 173*0b57cec5SDimitry Andric RunScriptBasedCommand(const char *impl_function, llvm::StringRef args, 174*0b57cec5SDimitry Andric ScriptedCommandSynchronicity synchronicity, 175*0b57cec5SDimitry Andric lldb_private::CommandReturnObject &cmd_retobj, 176*0b57cec5SDimitry Andric Status &error, 177*0b57cec5SDimitry Andric const lldb_private::ExecutionContext &exe_ctx) override; 178*0b57cec5SDimitry Andric 179*0b57cec5SDimitry Andric bool RunScriptBasedCommand( 180*0b57cec5SDimitry Andric StructuredData::GenericSP impl_obj_sp, llvm::StringRef args, 181*0b57cec5SDimitry Andric ScriptedCommandSynchronicity synchronicity, 182*0b57cec5SDimitry Andric lldb_private::CommandReturnObject &cmd_retobj, Status &error, 183*0b57cec5SDimitry Andric const lldb_private::ExecutionContext &exe_ctx) override; 184*0b57cec5SDimitry Andric 185*0b57cec5SDimitry Andric Status GenerateFunction(const char *signature, 186*0b57cec5SDimitry Andric const StringList &input) override; 187*0b57cec5SDimitry Andric 188480093f4SDimitry Andric Status GenerateBreakpointCommandCallbackData( 189480093f4SDimitry Andric StringList &input, 190480093f4SDimitry Andric std::string &output, 191480093f4SDimitry Andric bool has_extra_args) override; 192*0b57cec5SDimitry Andric 193*0b57cec5SDimitry Andric bool GenerateWatchpointCommandCallbackData(StringList &input, 194*0b57cec5SDimitry Andric std::string &output) override; 195*0b57cec5SDimitry Andric 196*0b57cec5SDimitry Andric bool GetScriptedSummary(const char *function_name, lldb::ValueObjectSP valobj, 197*0b57cec5SDimitry Andric StructuredData::ObjectSP &callee_wrapper_sp, 198*0b57cec5SDimitry Andric const TypeSummaryOptions &options, 199*0b57cec5SDimitry Andric std::string &retval) override; 200*0b57cec5SDimitry Andric 201*0b57cec5SDimitry Andric bool GetDocumentationForItem(const char *item, std::string &dest) override; 202*0b57cec5SDimitry Andric 203*0b57cec5SDimitry Andric bool GetShortHelpForCommandObject(StructuredData::GenericSP cmd_obj_sp, 204*0b57cec5SDimitry Andric std::string &dest) override; 205*0b57cec5SDimitry Andric 206*0b57cec5SDimitry Andric uint32_t 207*0b57cec5SDimitry Andric GetFlagsForCommandObject(StructuredData::GenericSP cmd_obj_sp) override; 208*0b57cec5SDimitry Andric 209*0b57cec5SDimitry Andric bool GetLongHelpForCommandObject(StructuredData::GenericSP cmd_obj_sp, 210*0b57cec5SDimitry Andric std::string &dest) override; 211*0b57cec5SDimitry Andric 212*0b57cec5SDimitry Andric bool CheckObjectExists(const char *name) override { 213*0b57cec5SDimitry Andric if (!name || !name[0]) 214*0b57cec5SDimitry Andric return false; 215*0b57cec5SDimitry Andric std::string temp; 216*0b57cec5SDimitry Andric return GetDocumentationForItem(name, temp); 217*0b57cec5SDimitry Andric } 218*0b57cec5SDimitry Andric 219*0b57cec5SDimitry Andric bool RunScriptFormatKeyword(const char *impl_function, Process *process, 220*0b57cec5SDimitry Andric std::string &output, Status &error) override; 221*0b57cec5SDimitry Andric 222*0b57cec5SDimitry Andric bool RunScriptFormatKeyword(const char *impl_function, Thread *thread, 223*0b57cec5SDimitry Andric std::string &output, Status &error) override; 224*0b57cec5SDimitry Andric 225*0b57cec5SDimitry Andric bool RunScriptFormatKeyword(const char *impl_function, Target *target, 226*0b57cec5SDimitry Andric std::string &output, Status &error) override; 227*0b57cec5SDimitry Andric 228*0b57cec5SDimitry Andric bool RunScriptFormatKeyword(const char *impl_function, StackFrame *frame, 229*0b57cec5SDimitry Andric std::string &output, Status &error) override; 230*0b57cec5SDimitry Andric 231*0b57cec5SDimitry Andric bool RunScriptFormatKeyword(const char *impl_function, ValueObject *value, 232*0b57cec5SDimitry Andric std::string &output, Status &error) override; 233*0b57cec5SDimitry Andric 234e8d8bef9SDimitry Andric bool LoadScriptingModule(const char *filename, bool init_session, 235*0b57cec5SDimitry Andric lldb_private::Status &error, 236e8d8bef9SDimitry Andric StructuredData::ObjectSP *module_sp = nullptr, 237e8d8bef9SDimitry Andric FileSpec extra_search_dir = {}) override; 238*0b57cec5SDimitry Andric 239*0b57cec5SDimitry Andric bool IsReservedWord(const char *word) override; 240*0b57cec5SDimitry Andric 241*0b57cec5SDimitry Andric std::unique_ptr<ScriptInterpreterLocker> AcquireInterpreterLock() override; 242*0b57cec5SDimitry Andric 243*0b57cec5SDimitry Andric void CollectDataForBreakpointCommandCallback( 244*0b57cec5SDimitry Andric std::vector<BreakpointOptions *> &bp_options_vec, 245*0b57cec5SDimitry Andric CommandReturnObject &result) override; 246*0b57cec5SDimitry Andric 247*0b57cec5SDimitry Andric void 248*0b57cec5SDimitry Andric CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options, 249*0b57cec5SDimitry Andric CommandReturnObject &result) override; 250*0b57cec5SDimitry Andric 251*0b57cec5SDimitry Andric /// Set the callback body text into the callback for the breakpoint. 252*0b57cec5SDimitry Andric Status SetBreakpointCommandCallback(BreakpointOptions *bp_options, 253*0b57cec5SDimitry Andric const char *callback_body) override; 254*0b57cec5SDimitry Andric 255480093f4SDimitry Andric Status SetBreakpointCommandCallbackFunction( 256480093f4SDimitry Andric BreakpointOptions *bp_options, 257480093f4SDimitry Andric const char *function_name, 258480093f4SDimitry Andric StructuredData::ObjectSP extra_args_sp) override; 259*0b57cec5SDimitry Andric 260*0b57cec5SDimitry Andric /// This one is for deserialization: 261*0b57cec5SDimitry Andric Status SetBreakpointCommandCallback( 262*0b57cec5SDimitry Andric BreakpointOptions *bp_options, 263*0b57cec5SDimitry Andric std::unique_ptr<BreakpointOptions::CommandData> &data_up) override; 264*0b57cec5SDimitry 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 270*0b57cec5SDimitry Andric /// Set a one-liner as the callback for the watchpoint. 271*0b57cec5SDimitry Andric void SetWatchpointCommandCallback(WatchpointOptions *wp_options, 272*0b57cec5SDimitry Andric const char *oneliner) override; 273*0b57cec5SDimitry Andric 274*0b57cec5SDimitry Andric const char *GetDictionaryName() { return m_dictionary_name.c_str(); } 275*0b57cec5SDimitry Andric 276*0b57cec5SDimitry Andric PyThreadState *GetThreadState() { return m_command_thread_state; } 277*0b57cec5SDimitry Andric 278*0b57cec5SDimitry Andric void SetThreadState(PyThreadState *s) { 279*0b57cec5SDimitry Andric if (s) 280*0b57cec5SDimitry Andric m_command_thread_state = s; 281*0b57cec5SDimitry Andric } 282*0b57cec5SDimitry Andric 283*0b57cec5SDimitry Andric // IOHandlerDelegate 284*0b57cec5SDimitry Andric void IOHandlerActivated(IOHandler &io_handler, bool interactive) override; 285*0b57cec5SDimitry Andric 286*0b57cec5SDimitry Andric void IOHandlerInputComplete(IOHandler &io_handler, 287*0b57cec5SDimitry Andric std::string &data) override; 288*0b57cec5SDimitry Andric 289*0b57cec5SDimitry Andric static lldb::ScriptInterpreterSP CreateInstance(Debugger &debugger); 290*0b57cec5SDimitry Andric 291*0b57cec5SDimitry Andric // PluginInterface protocol 292*0b57cec5SDimitry Andric lldb_private::ConstString GetPluginName() override; 293*0b57cec5SDimitry Andric 294*0b57cec5SDimitry Andric uint32_t GetPluginVersion() override; 295*0b57cec5SDimitry Andric 296*0b57cec5SDimitry Andric class Locker : public ScriptInterpreterLocker { 297*0b57cec5SDimitry Andric public: 298*0b57cec5SDimitry Andric enum OnEntry { 299*0b57cec5SDimitry Andric AcquireLock = 0x0001, 300*0b57cec5SDimitry Andric InitSession = 0x0002, 301*0b57cec5SDimitry Andric InitGlobals = 0x0004, 302*0b57cec5SDimitry Andric NoSTDIN = 0x0008 303*0b57cec5SDimitry Andric }; 304*0b57cec5SDimitry Andric 305*0b57cec5SDimitry Andric enum OnLeave { 306*0b57cec5SDimitry Andric FreeLock = 0x0001, 307*0b57cec5SDimitry Andric FreeAcquiredLock = 0x0002, // do not free the lock if we already held it 308*0b57cec5SDimitry Andric // when calling constructor 309*0b57cec5SDimitry Andric TearDownSession = 0x0004 310*0b57cec5SDimitry Andric }; 311*0b57cec5SDimitry Andric 3129dba64beSDimitry Andric Locker(ScriptInterpreterPythonImpl *py_interpreter, 313*0b57cec5SDimitry 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); 317*0b57cec5SDimitry Andric 318*0b57cec5SDimitry Andric ~Locker() override; 319*0b57cec5SDimitry Andric 320*0b57cec5SDimitry Andric private: 321*0b57cec5SDimitry Andric bool DoAcquireLock(); 322*0b57cec5SDimitry Andric 3239dba64beSDimitry Andric bool DoInitSession(uint16_t on_entry_flags, lldb::FileSP in, 3249dba64beSDimitry Andric lldb::FileSP out, lldb::FileSP err); 325*0b57cec5SDimitry Andric 326*0b57cec5SDimitry Andric bool DoFreeLock(); 327*0b57cec5SDimitry Andric 328*0b57cec5SDimitry Andric bool DoTearDownSession(); 329*0b57cec5SDimitry Andric 330*0b57cec5SDimitry Andric bool m_teardown_session; 331*0b57cec5SDimitry Andric ScriptInterpreterPythonImpl *m_python_interpreter; 332*0b57cec5SDimitry Andric PyGILState_STATE m_GILState; 333*0b57cec5SDimitry Andric }; 334*0b57cec5SDimitry Andric 335*0b57cec5SDimitry Andric static bool BreakpointCallbackFunction(void *baton, 336*0b57cec5SDimitry Andric StoppointCallbackContext *context, 337*0b57cec5SDimitry Andric lldb::user_id_t break_id, 338*0b57cec5SDimitry Andric lldb::user_id_t break_loc_id); 339*0b57cec5SDimitry Andric static bool WatchpointCallbackFunction(void *baton, 340*0b57cec5SDimitry Andric StoppointCallbackContext *context, 341*0b57cec5SDimitry Andric lldb::user_id_t watch_id); 342*0b57cec5SDimitry Andric static void InitializePrivate(); 343*0b57cec5SDimitry Andric 344*0b57cec5SDimitry Andric class SynchronicityHandler { 345*0b57cec5SDimitry Andric private: 346*0b57cec5SDimitry Andric lldb::DebuggerSP m_debugger_sp; 347*0b57cec5SDimitry Andric ScriptedCommandSynchronicity m_synch_wanted; 348*0b57cec5SDimitry Andric bool m_old_asynch; 349*0b57cec5SDimitry Andric 350*0b57cec5SDimitry Andric public: 351*0b57cec5SDimitry Andric SynchronicityHandler(lldb::DebuggerSP, ScriptedCommandSynchronicity); 352*0b57cec5SDimitry Andric 353*0b57cec5SDimitry Andric ~SynchronicityHandler(); 354*0b57cec5SDimitry Andric }; 355*0b57cec5SDimitry Andric 356*0b57cec5SDimitry Andric enum class AddLocation { Beginning, End }; 357*0b57cec5SDimitry Andric 358*0b57cec5SDimitry Andric static void AddToSysPath(AddLocation location, std::string path); 359*0b57cec5SDimitry Andric 3609dba64beSDimitry Andric bool EnterSession(uint16_t on_entry_flags, lldb::FileSP in, lldb::FileSP out, 3619dba64beSDimitry Andric lldb::FileSP err); 362*0b57cec5SDimitry Andric 363*0b57cec5SDimitry Andric void LeaveSession(); 364*0b57cec5SDimitry Andric 365*0b57cec5SDimitry Andric uint32_t IsExecutingPython() const { return m_lock_count > 0; } 366*0b57cec5SDimitry Andric 367*0b57cec5SDimitry Andric uint32_t IncrementLockCount() { return ++m_lock_count; } 368*0b57cec5SDimitry Andric 369*0b57cec5SDimitry Andric uint32_t DecrementLockCount() { 370*0b57cec5SDimitry Andric if (m_lock_count > 0) 371*0b57cec5SDimitry Andric --m_lock_count; 372*0b57cec5SDimitry Andric return m_lock_count; 373*0b57cec5SDimitry Andric } 374*0b57cec5SDimitry Andric 375*0b57cec5SDimitry Andric enum ActiveIOHandler { 376*0b57cec5SDimitry Andric eIOHandlerNone, 377*0b57cec5SDimitry Andric eIOHandlerBreakpoint, 378*0b57cec5SDimitry Andric eIOHandlerWatchpoint 379*0b57cec5SDimitry Andric }; 380*0b57cec5SDimitry Andric 3819dba64beSDimitry Andric python::PythonModule &GetMainModule(); 382*0b57cec5SDimitry Andric 3839dba64beSDimitry Andric python::PythonDictionary &GetSessionDictionary(); 384*0b57cec5SDimitry Andric 3859dba64beSDimitry Andric python::PythonDictionary &GetSysModuleDictionary(); 386*0b57cec5SDimitry Andric 387480093f4SDimitry Andric llvm::Expected<unsigned> GetMaxPositionalArgumentsForCallable( 388480093f4SDimitry Andric const llvm::StringRef &callable_name) override; 389480093f4SDimitry Andric 390*0b57cec5SDimitry Andric bool GetEmbeddedInterpreterModuleObjects(); 391*0b57cec5SDimitry Andric 3929dba64beSDimitry Andric bool SetStdHandle(lldb::FileSP file, const char *py_name, 3939dba64beSDimitry Andric python::PythonObject &save_file, const char *mode); 394*0b57cec5SDimitry 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; 403*0b57cec5SDimitry Andric std::string m_dictionary_name; 404*0b57cec5SDimitry Andric ActiveIOHandler m_active_io_handler; 405*0b57cec5SDimitry Andric bool m_session_is_active; 4065ffd83dbSDimitry Andric bool m_pty_secondary_is_open; 407*0b57cec5SDimitry Andric bool m_valid_session; 408*0b57cec5SDimitry Andric uint32_t m_lock_count; 409*0b57cec5SDimitry Andric PyThreadState *m_command_thread_state; 410*0b57cec5SDimitry Andric }; 411*0b57cec5SDimitry Andric 412*0b57cec5SDimitry Andric class IOHandlerPythonInterpreter : public IOHandler { 413*0b57cec5SDimitry Andric public: 414*0b57cec5SDimitry Andric IOHandlerPythonInterpreter(Debugger &debugger, 415*0b57cec5SDimitry Andric ScriptInterpreterPythonImpl *python) 416*0b57cec5SDimitry Andric : IOHandler(debugger, IOHandler::Type::PythonInterpreter), 417*0b57cec5SDimitry Andric m_python(python) {} 418*0b57cec5SDimitry Andric 419*0b57cec5SDimitry Andric ~IOHandlerPythonInterpreter() override {} 420*0b57cec5SDimitry Andric 421*0b57cec5SDimitry Andric ConstString GetControlSequence(char ch) override { 422*0b57cec5SDimitry Andric if (ch == 'd') 423*0b57cec5SDimitry Andric return ConstString("quit()\n"); 424*0b57cec5SDimitry Andric return ConstString(); 425*0b57cec5SDimitry Andric } 426*0b57cec5SDimitry Andric 427*0b57cec5SDimitry Andric void Run() override { 428*0b57cec5SDimitry Andric if (m_python) { 429*0b57cec5SDimitry Andric int stdin_fd = GetInputFD(); 430*0b57cec5SDimitry Andric if (stdin_fd >= 0) { 431*0b57cec5SDimitry Andric Terminal terminal(stdin_fd); 432*0b57cec5SDimitry Andric TerminalState terminal_state; 433*0b57cec5SDimitry Andric const bool is_a_tty = terminal.IsATerminal(); 434*0b57cec5SDimitry Andric 435*0b57cec5SDimitry Andric if (is_a_tty) { 436*0b57cec5SDimitry Andric terminal_state.Save(stdin_fd, false); 437*0b57cec5SDimitry Andric terminal.SetCanonical(false); 438*0b57cec5SDimitry Andric terminal.SetEcho(true); 439*0b57cec5SDimitry Andric } 440*0b57cec5SDimitry Andric 441*0b57cec5SDimitry Andric ScriptInterpreterPythonImpl::Locker locker( 442*0b57cec5SDimitry Andric m_python, 443*0b57cec5SDimitry Andric ScriptInterpreterPythonImpl::Locker::AcquireLock | 444*0b57cec5SDimitry Andric ScriptInterpreterPythonImpl::Locker::InitSession | 445*0b57cec5SDimitry Andric ScriptInterpreterPythonImpl::Locker::InitGlobals, 446*0b57cec5SDimitry Andric ScriptInterpreterPythonImpl::Locker::FreeAcquiredLock | 447*0b57cec5SDimitry Andric ScriptInterpreterPythonImpl::Locker::TearDownSession); 448*0b57cec5SDimitry Andric 449*0b57cec5SDimitry Andric // The following call drops into the embedded interpreter loop and 450*0b57cec5SDimitry Andric // stays there until the user chooses to exit from the Python 451*0b57cec5SDimitry Andric // interpreter. This embedded interpreter will, as any Python code that 452*0b57cec5SDimitry Andric // performs I/O, unlock the GIL before a system call that can hang, and 453*0b57cec5SDimitry Andric // lock it when the syscall has returned. 454*0b57cec5SDimitry Andric 455*0b57cec5SDimitry Andric // We need to surround the call to the embedded interpreter with calls 456*0b57cec5SDimitry Andric // to PyGILState_Ensure and PyGILState_Release (using the Locker 457*0b57cec5SDimitry Andric // above). This is because Python has a global lock which must be held 458*0b57cec5SDimitry Andric // whenever we want to touch any Python objects. Otherwise, if the user 459*0b57cec5SDimitry Andric // calls Python code, the interpreter state will be off, and things 460*0b57cec5SDimitry Andric // could hang (it's happened before). 461*0b57cec5SDimitry Andric 462*0b57cec5SDimitry Andric StreamString run_string; 463*0b57cec5SDimitry Andric run_string.Printf("run_python_interpreter (%s)", 464*0b57cec5SDimitry Andric m_python->GetDictionaryName()); 465*0b57cec5SDimitry Andric PyRun_SimpleString(run_string.GetData()); 466*0b57cec5SDimitry Andric 467*0b57cec5SDimitry Andric if (is_a_tty) 468*0b57cec5SDimitry Andric terminal_state.Restore(); 469*0b57cec5SDimitry Andric } 470*0b57cec5SDimitry Andric } 471*0b57cec5SDimitry Andric SetIsDone(true); 472*0b57cec5SDimitry Andric } 473*0b57cec5SDimitry Andric 474*0b57cec5SDimitry Andric void Cancel() override {} 475*0b57cec5SDimitry Andric 476*0b57cec5SDimitry Andric bool Interrupt() override { return m_python->Interrupt(); } 477*0b57cec5SDimitry Andric 478*0b57cec5SDimitry Andric void GotEOF() override {} 479*0b57cec5SDimitry Andric 480*0b57cec5SDimitry Andric protected: 481*0b57cec5SDimitry Andric ScriptInterpreterPythonImpl *m_python; 482*0b57cec5SDimitry Andric }; 483*0b57cec5SDimitry Andric 484*0b57cec5SDimitry Andric } // namespace lldb_private 485*0b57cec5SDimitry Andric 486*0b57cec5SDimitry Andric #endif 487