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