Index: CMakeLists.txt =================================================================== --- CMakeLists.txt +++ CMakeLists.txt @@ -25,7 +25,7 @@ "Enables using new Python scripts for SWIG API generation .") set(LLDB_SOURCE_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/source") -set(LLDB_INCLUDE_ROOT "${LLDB_INCLUDE_ROOT}/include") +set(LLDB_INCLUDE_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/include") # If we are not building as a part of LLVM, build LLDB as an # standalone project, using LLVM as an external library: Index: cmake/LLDBDependencies.cmake =================================================================== --- cmake/LLDBDependencies.cmake +++ cmake/LLDBDependencies.cmake @@ -53,6 +53,7 @@ lldbPluginOSPython lldbPluginMemoryHistoryASan lldbPluginInstrumentationRuntimeAddressSanitizer + lldbScriptInterpreter ) # Windows-only libraries @@ -107,6 +108,10 @@ ) endif() +if (NOT LLDB_DISABLE_PYTHON) + list(APPEND LLDB_USED_LIBS lldbPythonInterpreter) +endif() + set( CLANG_USED_LIBS clangAnalysis clangAST Index: include/lldb/API/SBBreakpointLocation.h =================================================================== --- include/lldb/API/SBBreakpointLocation.h +++ include/lldb/API/SBBreakpointLocation.h @@ -101,9 +101,6 @@ private: friend class SBBreakpoint; -#ifndef LLDB_DISABLE_PYTHON - friend class lldb_private::ScriptInterpreterPython; -#endif void SetLocation (const lldb::BreakpointLocationSP &break_loc_sp); Index: include/lldb/API/SBCommandInterpreter.h =================================================================== --- include/lldb/API/SBCommandInterpreter.h +++ include/lldb/API/SBCommandInterpreter.h @@ -229,9 +229,6 @@ private: friend class SBDebugger; - static void - InitializeSWIG (); - lldb_private::CommandInterpreter *m_opaque_ptr; }; Index: include/lldb/API/SBFrame.h =================================================================== --- include/lldb/API/SBFrame.h +++ include/lldb/API/SBFrame.h @@ -213,9 +213,6 @@ friend class SBInstruction; friend class SBThread; friend class SBValue; -#ifndef LLDB_DISABLE_PYTHON - friend class lldb_private::ScriptInterpreterPython; -#endif lldb::StackFrameSP GetFrameSP() const; Index: include/lldb/DataFormatters/TypeSummary.h =================================================================== --- include/lldb/DataFormatters/TypeSummary.h +++ include/lldb/DataFormatters/TypeSummary.h @@ -26,7 +26,7 @@ #include "lldb/Core/Error.h" #include "lldb/Core/FormatEntity.h" #include "lldb/Core/ValueObject.h" -#include "lldb/Interpreter/ScriptInterpreterPython.h" +#include "lldb/ScriptInterpreter/ScriptInterpreter.h" #include "lldb/Symbol/Type.h" namespace lldb_private { Index: include/lldb/Interpreter/CommandInterpreter.h =================================================================== --- include/lldb/Interpreter/CommandInterpreter.h +++ include/lldb/Interpreter/CommandInterpreter.h @@ -14,6 +14,7 @@ // C++ Includes // Other libraries and framework includes // Project includes +#include "lldb/lldb-forward.h" #include "lldb/lldb-private.h" #include "lldb/Core/Broadcaster.h" #include "lldb/Core/Debugger.h" @@ -21,7 +22,6 @@ #include "lldb/Core/Log.h" #include "lldb/Interpreter/CommandHistory.h" #include "lldb/Interpreter/CommandObject.h" -#include "lldb/Interpreter/ScriptInterpreter.h" #include "lldb/Core/Event.h" #include "lldb/Interpreter/Args.h" #include "lldb/Core/StringList.h" @@ -696,7 +696,7 @@ OptionArgMap m_alias_options; // Stores any options (with or without arguments) that go with any alias. CommandHistory m_command_history; std::string m_repeat_command; // Stores the command that will be executed for an empty command string. - std::unique_ptr m_script_interpreter_ap; + lldb::ScriptInterpreterUP m_script_interpreter_ap; lldb::IOHandlerSP m_command_io_handler_sp; char m_comment_char; bool m_batch_command_mode; Index: include/lldb/ScriptInterpreter/Python/PythonDataObjects.h =================================================================== --- include/lldb/ScriptInterpreter/Python/PythonDataObjects.h +++ include/lldb/ScriptInterpreter/Python/PythonDataObjects.h @@ -21,209 +21,173 @@ #include "lldb/Interpreter/OptionValue.h" #include "lldb/lldb-python.h" -namespace lldb_private { - - class PythonObject +namespace lldb_private +{ + +class PythonObject +{ + public: + PythonObject() + : m_py_obj(NULL) { - public: - PythonObject () : - m_py_obj(NULL) - { - } - - explicit PythonObject (PyObject* py_obj) : - m_py_obj(NULL) - { - Reset (py_obj); - } - - PythonObject (const PythonObject &rhs) : - m_py_obj(NULL) - { - Reset (rhs.m_py_obj); - } - - explicit PythonObject (const lldb::ScriptInterpreterObjectSP &script_object_sp); + } - virtual - ~PythonObject () - { - Reset (NULL); - } + explicit PythonObject(PyObject *py_obj) + : m_py_obj(NULL) + { + Reset(py_obj); + } - bool - Reset (const PythonObject &object) - { - return Reset(object.get()); - } + PythonObject(const PythonObject &rhs) + : m_py_obj(NULL) + { + Reset(rhs.m_py_obj); + } - virtual bool - Reset (PyObject* py_obj = NULL) - { - if (py_obj != m_py_obj) - { - if (Py_IsInitialized()) - Py_XDECREF(m_py_obj); - m_py_obj = py_obj; - if (Py_IsInitialized()) - Py_XINCREF(m_py_obj); - } - return true; - } - - void - Dump () const - { - if (m_py_obj) - _PyObject_Dump (m_py_obj); - else - puts ("NULL"); - } - - void - Dump (Stream &strm) const; + explicit PythonObject(const lldb::ScriptInterpreterObjectSP &script_object_sp); - PyObject* - get () const - { - return m_py_obj; - } + virtual ~PythonObject() { Reset(NULL); } - PythonString - Repr (); - - PythonString - Str (); - - explicit operator bool () const - { - return m_py_obj != NULL; - } - - bool - IsNULLOrNone () const; - - protected: - PyObject* m_py_obj; - }; - - class PythonString: public PythonObject + bool + Reset(const PythonObject &object) { - public: - - PythonString (); - PythonString (PyObject *o); - PythonString (const PythonObject &object); - PythonString (const lldb::ScriptInterpreterObjectSP &script_object_sp); - PythonString (const char* string); - virtual ~PythonString (); - - virtual bool - Reset (PyObject* py_obj = NULL); - - const char* - GetString() const; - - size_t - GetSize() const; - - void - SetString (const char* string); - }; - - class PythonInteger: public PythonObject + return Reset(object.get()); + } + + virtual bool + Reset(PyObject *py_obj = NULL) { - public: - - PythonInteger (); - PythonInteger (PyObject* py_obj); - PythonInteger (const PythonObject &object); - PythonInteger (const lldb::ScriptInterpreterObjectSP &script_object_sp); - PythonInteger (int64_t value); - virtual ~PythonInteger (); - - virtual bool - Reset (PyObject* py_obj = NULL); - - int64_t - GetInteger(); - - void - SetInteger (int64_t value); - }; - - class PythonList: public PythonObject + if (py_obj != m_py_obj) + { + if (Py_IsInitialized()) + Py_XDECREF(m_py_obj); + m_py_obj = py_obj; + if (Py_IsInitialized()) + Py_XINCREF(m_py_obj); + } + return true; + } + + void + Dump() const { - public: - - PythonList (bool create_empty); - PythonList (PyObject* py_obj); - PythonList (const PythonObject &object); - PythonList (const lldb::ScriptInterpreterObjectSP &script_object_sp); - PythonList (uint32_t count); - virtual ~PythonList (); - - virtual bool - Reset (PyObject* py_obj = NULL); - - uint32_t - GetSize(); - - PythonObject - GetItemAtIndex (uint32_t index); - - void - SetItemAtIndex (uint32_t index, const PythonObject &object); - - void - AppendItem (const PythonObject &object); - }; - - class PythonDictionary: public PythonObject + if (m_py_obj) + _PyObject_Dump(m_py_obj); + else + puts("NULL"); + } + + void Dump(Stream &strm) const; + + PyObject * + get() const { - public: - - explicit PythonDictionary (bool create_empty); - PythonDictionary (PyObject* object); - PythonDictionary (const PythonObject &object); - PythonDictionary (const lldb::ScriptInterpreterObjectSP &script_object_sp); - virtual ~PythonDictionary (); - - virtual bool - Reset (PyObject* object = NULL); - - uint32_t GetSize(); - - PythonObject - GetItemForKey (const PythonString &key) const; - - const char * - GetItemForKeyAsString (const PythonString &key, const char *fail_value = NULL) const; - - int64_t - GetItemForKeyAsInteger (const PythonString &key, int64_t fail_value = 0) const; - - PythonObject - GetItemForKey (const char *key) const; - - typedef bool (*DictionaryIteratorCallback)(PythonString* key, PythonDictionary* dict); - - PythonList - GetKeys () const; - - PythonString - GetKeyAtPosition (uint32_t pos) const; - - PythonObject - GetValueAtPosition (uint32_t pos) const; - - void - SetItemForKey (const PythonString &key, PyObject *value); - - void - SetItemForKey (const PythonString &key, const PythonObject& value); - }; - + return m_py_obj; + } + + PythonString Repr(); + + PythonString Str(); + + explicit operator bool() const { return m_py_obj != NULL; } + + bool IsNULLOrNone() const; + + protected: + PyObject *m_py_obj; +}; + +class PythonString : public PythonObject +{ + public: + PythonString(); + PythonString(PyObject *o); + PythonString(const PythonObject &object); + PythonString(const lldb::ScriptInterpreterObjectSP &script_object_sp); + PythonString(const char *string); + virtual ~PythonString(); + + virtual bool Reset(PyObject *py_obj = NULL); + + const char *GetString() const; + + size_t GetSize() const; + + void SetString(const char *string); +}; + +class PythonInteger : public PythonObject +{ + public: + PythonInteger(); + PythonInteger(PyObject *py_obj); + PythonInteger(const PythonObject &object); + PythonInteger(const lldb::ScriptInterpreterObjectSP &script_object_sp); + PythonInteger(int64_t value); + virtual ~PythonInteger(); + + virtual bool Reset(PyObject *py_obj = NULL); + + int64_t GetInteger(); + + void SetInteger(int64_t value); +}; + +class PythonList : public PythonObject +{ + public: + PythonList(bool create_empty); + PythonList(PyObject *py_obj); + PythonList(const PythonObject &object); + PythonList(const lldb::ScriptInterpreterObjectSP &script_object_sp); + PythonList(uint32_t count); + virtual ~PythonList(); + + virtual bool Reset(PyObject *py_obj = NULL); + + uint32_t GetSize(); + + PythonObject GetItemAtIndex(uint32_t index); + + void SetItemAtIndex(uint32_t index, const PythonObject &object); + + void AppendItem(const PythonObject &object); +}; + +class PythonDictionary : public PythonObject +{ + public: + explicit PythonDictionary(bool create_empty); + PythonDictionary(PyObject *object); + PythonDictionary(const PythonObject &object); + PythonDictionary(const lldb::ScriptInterpreterObjectSP &script_object_sp); + virtual ~PythonDictionary(); + + virtual bool Reset(PyObject *object = NULL); + + uint32_t GetSize(); + + PythonObject GetItemForKey(const PythonString &key) const; + + const char *GetItemForKeyAsString(const PythonString &key, const char *fail_value = NULL) const; + + int64_t GetItemForKeyAsInteger(const PythonString &key, int64_t fail_value = 0) const; + + PythonObject GetItemForKey(const char *key) const; + + typedef bool (*DictionaryIteratorCallback)(PythonString *key, PythonDictionary *dict); + + PythonList GetKeys() const; + + PythonString GetKeyAtPosition(uint32_t pos) const; + + PythonObject GetValueAtPosition(uint32_t pos) const; + + void SetItemForKey(const PythonString &key, PyObject *value); + + void SetItemForKey(const PythonString &key, const PythonObject &value); +}; + } // namespace lldb_private -#endif // liblldb_PythonDataObjects_h_ +#endif // liblldb_PythonDataObjects_h_ Index: include/lldb/ScriptInterpreter/Python/PythonPointer.h =================================================================== --- include/lldb/ScriptInterpreter/Python/PythonPointer.h +++ include/lldb/ScriptInterpreter/Python/PythonPointer.h @@ -14,29 +14,30 @@ #include "lldb/lldb-python.h" -namespace lldb_private { +namespace lldb_private +{ -template -class PythonPointer +template class PythonPointer { -public: - typedef PyObject* element_type; -private: - element_type* ptr_; + public: + typedef PyObject *element_type; + + private: + element_type *ptr_; bool my_ref; -public: - - PythonPointer(element_type p, bool steal_ref = false) : - ptr_(p), - my_ref(!steal_ref) + + public: + PythonPointer(element_type p, bool steal_ref = false) + : ptr_(p) + , my_ref(!steal_ref) { if (my_ref) Py_INCREF(ptr_); } - - PythonPointer(const PythonPointer& r, bool steal_ref = false) : - ptr_(r.ptr_), - my_ref(!steal_ref) + + PythonPointer(const PythonPointer &r, bool steal_ref = false) + : ptr_(r.ptr_) + , my_ref(!steal_ref) { if (my_ref) Py_INCREF(ptr_); @@ -47,27 +48,39 @@ if (my_ref) Py_XDECREF(ptr_); } - + PythonPointer StealReference() { - return PythonPointer(ptr_,true); + return PythonPointer(ptr_, true); } - + PythonPointer DuplicateReference() { return PythonPointer(ptr_, false); } - element_type get() const {return ptr_;} - - bool IsNull() { return ptr_ == NULL; } - bool IsNone() { return ptr_ == Py_None; } - - operator PyObject* () { return ptr_; } + element_type + get() const + { + return ptr_; + } + + bool + IsNull() + { + return ptr_ == NULL; + } + bool + IsNone() + { + return ptr_ == Py_None; + } + + operator PyObject *() { return ptr_; } }; } // namespace lldb -#endif // utility_PythonPointer_h_ +#endif // utility_PythonPointer_h_ Index: include/lldb/ScriptInterpreter/Python/ScriptInterpreterPython.h =================================================================== --- include/lldb/ScriptInterpreter/Python/ScriptInterpreterPython.h +++ include/lldb/ScriptInterpreter/Python/ScriptInterpreterPython.h @@ -7,7 +7,6 @@ // //===----------------------------------------------------------------------===// - #ifndef liblldb_ScriptInterpreterPython_h_ #define liblldb_ScriptInterpreterPython_h_ @@ -20,307 +19,174 @@ #include "lldb/lldb-python.h" #include "lldb/lldb-private.h" #include "lldb/Core/IOHandler.h" -#include "lldb/Interpreter/ScriptInterpreter.h" -#include "lldb/Interpreter/PythonDataObjects.h" +#include "lldb/ScriptInterpreter/ScriptInterpreter.h" +#include "lldb/ScriptInterpreter/Python/PythonDataObjects.h" #include "lldb/Host/Terminal.h" class IOHandlerPythonInterpreter; -namespace lldb_private { - -class ScriptInterpreterPython : - public ScriptInterpreter, - public IOHandlerDelegateMultiline +namespace lldb_private { -public: +class ScriptInterpreterPython : public ScriptInterpreter, public IOHandlerDelegateMultiline +{ + public: friend class IOHandlerPythonInterpreter; - ScriptInterpreterPython (CommandInterpreter &interpreter); + ScriptInterpreterPython(CommandInterpreter &interpreter); - ~ScriptInterpreterPython (); + ~ScriptInterpreterPython(); - bool - Interrupt() override; + bool Interrupt() override; - bool - ExecuteOneLine (const char *command, - CommandReturnObject *result, - const ExecuteScriptOptions &options = ExecuteScriptOptions()) override; + bool ExecuteOneLine(const char *command, CommandReturnObject *result, + const ExecuteScriptOptions &options = ExecuteScriptOptions()) override; - void - ExecuteInterpreterLoop () override; + void ExecuteInterpreterLoop() override; - bool - ExecuteOneLineWithReturn (const char *in_string, - ScriptInterpreter::ScriptReturnType return_type, - void *ret_value, - const ExecuteScriptOptions &options = ExecuteScriptOptions()) override; + bool ExecuteOneLineWithReturn(const char *in_string, ScriptInterpreter::ScriptReturnType return_type, void *ret_value, + const ExecuteScriptOptions &options = ExecuteScriptOptions()) override; - lldb_private::Error - ExecuteMultipleLines (const char *in_string, - const ExecuteScriptOptions &options = ExecuteScriptOptions()) override; + lldb_private::Error ExecuteMultipleLines(const char *in_string, const ExecuteScriptOptions &options = ExecuteScriptOptions()) override; - Error - ExportFunctionDefinitionToInterpreter (StringList &function_def) override; + Error ExportFunctionDefinitionToInterpreter(StringList &function_def) override; + + bool GenerateTypeScriptFunction(StringList &input, std::string &output, const void *name_token = NULL) override; + + bool GenerateTypeSynthClass(StringList &input, std::string &output, const void *name_token = NULL) override; + + bool GenerateTypeSynthClass(const char *oneliner, std::string &output, const void *name_token = NULL) override; - bool - GenerateTypeScriptFunction (StringList &input, std::string& output, const void* name_token = NULL) override; - - bool - GenerateTypeSynthClass (StringList &input, std::string& output, const void* name_token = NULL) override; - - bool - GenerateTypeSynthClass (const char* oneliner, std::string& output, const void* name_token = NULL) override; - // use this if the function code is just a one-liner script - bool - GenerateTypeScriptFunction (const char* oneliner, std::string& output, const void* name_token = NULL) override; - - bool - GenerateScriptAliasFunction (StringList &input, std::string& output) override; - - lldb::ScriptInterpreterObjectSP - CreateSyntheticScriptedProvider (const char *class_name, - lldb::ValueObjectSP valobj) override; + bool GenerateTypeScriptFunction(const char *oneliner, std::string &output, const void *name_token = NULL) override; - lldb::ScriptInterpreterObjectSP - CreateScriptedThreadPlan (const char *class_name, - lldb::ThreadPlanSP thread_plan) override; + bool GenerateScriptAliasFunction(StringList &input, std::string &output) override; - bool - ScriptedThreadPlanExplainsStop (lldb::ScriptInterpreterObjectSP implementor_sp, - Event *event, - bool &script_error) override; - bool - ScriptedThreadPlanShouldStop (lldb::ScriptInterpreterObjectSP implementor_sp, - Event *event, - bool &script_error) override; - lldb::StateType - ScriptedThreadPlanGetRunState (lldb::ScriptInterpreterObjectSP implementor_sp, - bool &script_error) override; - - lldb::ScriptInterpreterObjectSP - OSPlugin_CreatePluginObject (const char *class_name, - lldb::ProcessSP process_sp) override; - - lldb::ScriptInterpreterObjectSP - OSPlugin_RegisterInfo (lldb::ScriptInterpreterObjectSP os_plugin_object_sp) override; - - lldb::ScriptInterpreterObjectSP - OSPlugin_ThreadsInfo (lldb::ScriptInterpreterObjectSP os_plugin_object_sp) override; - - lldb::ScriptInterpreterObjectSP - OSPlugin_RegisterContextData (lldb::ScriptInterpreterObjectSP os_plugin_object_sp, - lldb::tid_t thread_id) override; - - lldb::ScriptInterpreterObjectSP - OSPlugin_CreateThread (lldb::ScriptInterpreterObjectSP os_plugin_object_sp, - lldb::tid_t tid, - lldb::addr_t context) override; - - lldb::ScriptInterpreterObjectSP - LoadPluginModule (const FileSpec& file_spec, - lldb_private::Error& error) override; - - lldb::ScriptInterpreterObjectSP - GetDynamicSettings (lldb::ScriptInterpreterObjectSP plugin_module_sp, - Target* target, - const char* setting_name, - lldb_private::Error& error) override; - - size_t - CalculateNumChildren (const lldb::ScriptInterpreterObjectSP& implementor) override; - - lldb::ValueObjectSP - GetChildAtIndex (const lldb::ScriptInterpreterObjectSP& implementor, uint32_t idx) override; - - int - GetIndexOfChildWithName (const lldb::ScriptInterpreterObjectSP& implementor, const char* child_name) override; - - bool - UpdateSynthProviderInstance (const lldb::ScriptInterpreterObjectSP& implementor) override; - - bool - MightHaveChildrenSynthProviderInstance (const lldb::ScriptInterpreterObjectSP& implementor) override; - - lldb::ValueObjectSP - GetSyntheticValue (const lldb::ScriptInterpreterObjectSP& implementor) override; - - bool - RunScriptBasedCommand(const char* impl_function, - const char* args, - ScriptedCommandSynchronicity synchronicity, - lldb_private::CommandReturnObject& cmd_retobj, - Error& error, - const lldb_private::ExecutionContext& exe_ctx) override; - - Error - GenerateFunction(const char *signature, const StringList &input) override; - - Error - GenerateBreakpointCommandCallbackData (StringList &input, std::string& output) override; + lldb::ScriptInterpreterObjectSP CreateSyntheticScriptedProvider(const char *class_name, lldb::ValueObjectSP valobj) override; - bool - GenerateWatchpointCommandCallbackData (StringList &input, std::string& output) override; - -// static size_t -// GenerateBreakpointOptionsCommandCallback (void *baton, -// InputReader &reader, -// lldb::InputReaderAction notification, -// const char *bytes, -// size_t bytes_len); -// -// static size_t -// GenerateWatchpointOptionsCommandCallback (void *baton, -// InputReader &reader, -// lldb::InputReaderAction notification, -// const char *bytes, -// size_t bytes_len); - - static bool - BreakpointCallbackFunction (void *baton, - StoppointCallbackContext *context, - lldb::user_id_t break_id, - lldb::user_id_t break_loc_id); - - static bool - WatchpointCallbackFunction (void *baton, - StoppointCallbackContext *context, - lldb::user_id_t watch_id); - - bool - GetScriptedSummary (const char *function_name, - lldb::ValueObjectSP valobj, - lldb::ScriptInterpreterObjectSP& callee_wrapper_sp, - const TypeSummaryOptions& options, - std::string& retval) override; - - void - Clear () override; + lldb::ScriptInterpreterObjectSP CreateScriptedThreadPlan(const char *class_name, lldb::ThreadPlanSP thread_plan) override; + + bool ScriptedThreadPlanExplainsStop(lldb::ScriptInterpreterObjectSP implementor_sp, Event *event, bool &script_error) override; + bool ScriptedThreadPlanShouldStop(lldb::ScriptInterpreterObjectSP implementor_sp, Event *event, bool &script_error) override; + lldb::StateType ScriptedThreadPlanGetRunState(lldb::ScriptInterpreterObjectSP implementor_sp, bool &script_error) override; + + lldb::ScriptInterpreterObjectSP OSPlugin_CreatePluginObject(const char *class_name, lldb::ProcessSP process_sp) override; + + lldb::ScriptInterpreterObjectSP OSPlugin_RegisterInfo(lldb::ScriptInterpreterObjectSP os_plugin_object_sp) override; + + lldb::ScriptInterpreterObjectSP OSPlugin_ThreadsInfo(lldb::ScriptInterpreterObjectSP os_plugin_object_sp) override; + + lldb::ScriptInterpreterObjectSP OSPlugin_RegisterContextData(lldb::ScriptInterpreterObjectSP os_plugin_object_sp, + lldb::tid_t thread_id) override; + + lldb::ScriptInterpreterObjectSP OSPlugin_CreateThread(lldb::ScriptInterpreterObjectSP os_plugin_object_sp, lldb::tid_t tid, + lldb::addr_t context) override; + + lldb::ScriptInterpreterObjectSP LoadPluginModule(const FileSpec &file_spec, lldb_private::Error &error) override; + + lldb::ScriptInterpreterObjectSP GetDynamicSettings(lldb::ScriptInterpreterObjectSP plugin_module_sp, Target *target, + const char *setting_name, lldb_private::Error &error) override; + + size_t CalculateNumChildren(const lldb::ScriptInterpreterObjectSP &implementor) override; + + lldb::ValueObjectSP GetChildAtIndex(const lldb::ScriptInterpreterObjectSP &implementor, uint32_t idx) override; + + int GetIndexOfChildWithName(const lldb::ScriptInterpreterObjectSP &implementor, const char *child_name) override; + + bool UpdateSynthProviderInstance(const lldb::ScriptInterpreterObjectSP &implementor) override; + + bool MightHaveChildrenSynthProviderInstance(const lldb::ScriptInterpreterObjectSP &implementor) override; + + lldb::ValueObjectSP GetSyntheticValue(const lldb::ScriptInterpreterObjectSP &implementor) override; + + bool RunScriptBasedCommand(const char *impl_function, const char *args, ScriptedCommandSynchronicity synchronicity, + lldb_private::CommandReturnObject &cmd_retobj, Error &error, + const lldb_private::ExecutionContext &exe_ctx) override; + + Error GenerateFunction(const char *signature, const StringList &input) override; + + Error GenerateBreakpointCommandCallbackData(StringList &input, std::string &output) override; + + bool GenerateWatchpointCommandCallbackData(StringList &input, std::string &output) override; + + // static size_t + // GenerateBreakpointOptionsCommandCallback (void *baton, + // InputReader &reader, + // lldb::InputReaderAction notification, + // const char *bytes, + // size_t bytes_len); + // + // static size_t + // GenerateWatchpointOptionsCommandCallback (void *baton, + // InputReader &reader, + // lldb::InputReaderAction notification, + // const char *bytes, + // size_t bytes_len); + + static bool BreakpointCallbackFunction(void *baton, StoppointCallbackContext *context, lldb::user_id_t break_id, + lldb::user_id_t break_loc_id); + + static bool WatchpointCallbackFunction(void *baton, StoppointCallbackContext *context, lldb::user_id_t watch_id); + + bool GetScriptedSummary(const char *function_name, lldb::ValueObjectSP valobj, lldb::ScriptInterpreterObjectSP &callee_wrapper_sp, + const TypeSummaryOptions &options, std::string &retval) override; + + void Clear() override; + + bool GetDocumentationForItem(const char *item, std::string &dest) override; bool - GetDocumentationForItem (const char* item, std::string& dest) override; - - bool - CheckObjectExists (const char* name) override + CheckObjectExists(const char *name) override { if (!name || !name[0]) return false; std::string temp; - return GetDocumentationForItem (name,temp); + return GetDocumentationForItem(name, temp); } - - bool - RunScriptFormatKeyword (const char* impl_function, - Process* process, - std::string& output, - Error& error) override; - bool - RunScriptFormatKeyword (const char* impl_function, - Thread* thread, - std::string& output, - Error& error) override; - - bool - RunScriptFormatKeyword (const char* impl_function, - Target* target, - std::string& output, - Error& error) override; - - bool - RunScriptFormatKeyword (const char* impl_function, - StackFrame* frame, - std::string& output, - Error& error) override; - - bool - RunScriptFormatKeyword (const char* impl_function, - ValueObject* value, - std::string& output, - Error& error) override; - - bool - LoadScriptingModule (const char* filename, - bool can_reload, - bool init_session, - lldb_private::Error& error, - lldb::ScriptInterpreterObjectSP* module_sp = nullptr) override; - - bool - IsReservedWord (const char* word) override; - - lldb::ScriptInterpreterObjectSP - MakeScriptObject (void* object) override; - - std::unique_ptr - AcquireInterpreterLock () override; - - void - CollectDataForBreakpointCommandCallback (std::vector &bp_options_vec, - CommandReturnObject &result) override; + bool RunScriptFormatKeyword(const char *impl_function, Process *process, std::string &output, Error &error) override; + + bool RunScriptFormatKeyword(const char *impl_function, Thread *thread, std::string &output, Error &error) override; + + bool RunScriptFormatKeyword(const char *impl_function, Target *target, std::string &output, Error &error) override; + + bool RunScriptFormatKeyword(const char *impl_function, StackFrame *frame, std::string &output, Error &error) override; + + bool RunScriptFormatKeyword(const char *impl_function, ValueObject *value, std::string &output, Error &error) override; + + bool LoadScriptingModule(const char *filename, bool can_reload, bool init_session, lldb_private::Error &error, + lldb::ScriptInterpreterObjectSP *module_sp = nullptr) override; + + bool IsReservedWord(const char *word) override; + + lldb::ScriptInterpreterObjectSP MakeScriptObject(void *object) override; + + std::unique_ptr AcquireInterpreterLock() override; - void - CollectDataForWatchpointCommandCallback (WatchpointOptions *wp_options, - CommandReturnObject &result) override; + void CollectDataForBreakpointCommandCallback(std::vector &bp_options_vec, CommandReturnObject &result) override; + + void CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options, CommandReturnObject &result) override; /// Set the callback body text into the callback for the breakpoint. - Error - SetBreakpointCommandCallback (BreakpointOptions *bp_options, - const char *callback_body) override; + Error SetBreakpointCommandCallback(BreakpointOptions *bp_options, const char *callback_body) override; - void - SetBreakpointCommandCallbackFunction (BreakpointOptions *bp_options, - const char *function_name) override; + void SetBreakpointCommandCallbackFunction(BreakpointOptions *bp_options, const char *function_name) override; /// Set a one-liner as the callback for the watchpoint. - void - SetWatchpointCommandCallback (WatchpointOptions *wp_options, - const char *oneliner) override; - - StringList - ReadCommandInputFromUser (FILE *in_file); - - virtual void - ResetOutputFileHandle (FILE *new_fh) override; - - static void - InitializePrivate (); - - static void - InitializeInterpreter (SWIGInitCallback python_swig_init_callback, - SWIGBreakpointCallbackFunction swig_breakpoint_callback, - SWIGWatchpointCallbackFunction swig_watchpoint_callback, - SWIGPythonTypeScriptCallbackFunction swig_typescript_callback, - SWIGPythonCreateSyntheticProvider swig_synthetic_script, - SWIGPythonCalculateNumChildren swig_calc_children, - SWIGPythonGetChildAtIndex swig_get_child_index, - SWIGPythonGetIndexOfChildWithName swig_get_index_child, - SWIGPythonCastPyObjectToSBValue swig_cast_to_sbvalue , - SWIGPythonGetValueObjectSPFromSBValue swig_get_valobj_sp_from_sbvalue, - SWIGPythonUpdateSynthProviderInstance swig_update_provider, - SWIGPythonMightHaveChildrenSynthProviderInstance swig_mighthavechildren_provider, - SWIGPythonGetValueSynthProviderInstance swig_getvalue_provider, - SWIGPythonCallCommand swig_call_command, - SWIGPythonCallModuleInit swig_call_module_init, - SWIGPythonCreateOSPlugin swig_create_os_plugin, - SWIGPythonScriptKeyword_Process swig_run_script_keyword_process, - SWIGPythonScriptKeyword_Thread swig_run_script_keyword_thread, - SWIGPythonScriptKeyword_Target swig_run_script_keyword_target, - SWIGPythonScriptKeyword_Frame swig_run_script_keyword_frame, - SWIGPythonScriptKeyword_Value swig_run_script_keyword_value, - SWIGPython_GetDynamicSetting swig_plugin_get, - SWIGPythonCreateScriptedThreadPlan swig_thread_plan_script, - SWIGPythonCallThreadPlan swig_call_thread_plan); + void SetWatchpointCommandCallback(WatchpointOptions *wp_options, const char *oneliner) override; + + StringList ReadCommandInputFromUser(FILE *in_file); + + virtual void ResetOutputFileHandle(FILE *new_fh) override; + + static void InitializePrivate(); const char * - GetDictionaryName () + GetDictionaryName() { return m_dictionary_name.c_str(); } - PyThreadState * GetThreadState() { @@ -328,7 +194,7 @@ } void - SetThreadState (PyThreadState *s) + SetThreadState(PyThreadState *s) { if (s) m_command_thread_state = s; @@ -337,125 +203,102 @@ //---------------------------------------------------------------------- // IOHandlerDelegate //---------------------------------------------------------------------- - void - IOHandlerActivated (IOHandler &io_handler) override; + void IOHandlerActivated(IOHandler &io_handler) override; - void - IOHandlerInputComplete (IOHandler &io_handler, std::string &data) override; + void IOHandlerInputComplete(IOHandler &io_handler, std::string &data) override; -protected: + protected: + bool EnterSession(uint16_t on_entry_flags, FILE *in, FILE *out, FILE *err); - bool - EnterSession (uint16_t on_entry_flags, - FILE *in, - FILE *out, - FILE *err); - - void - LeaveSession (); - - void - SaveTerminalState (int fd); + void LeaveSession(); + + void SaveTerminalState(int fd); + + void RestoreTerminalState(); - void - RestoreTerminalState (); - class SynchronicityHandler { - private: - lldb::DebuggerSP m_debugger_sp; + private: + lldb::DebuggerSP m_debugger_sp; ScriptedCommandSynchronicity m_synch_wanted; - bool m_old_asynch; - public: - SynchronicityHandler(lldb::DebuggerSP, - ScriptedCommandSynchronicity); + bool m_old_asynch; + + public: + SynchronicityHandler(lldb::DebuggerSP, ScriptedCommandSynchronicity); ~SynchronicityHandler(); }; - + class ScriptInterpreterPythonObject : public ScriptInterpreterObject { - public: - ScriptInterpreterPythonObject() : - ScriptInterpreterObject() - {} - - ScriptInterpreterPythonObject(void* obj) : - ScriptInterpreterObject(obj) + public: + ScriptInterpreterPythonObject() + : ScriptInterpreterObject() { - Py_XINCREF(m_object); } - - explicit operator bool () + + ScriptInterpreterPythonObject(void *obj) + : ScriptInterpreterObject(obj) { - return m_object && m_object != Py_None; + Py_XINCREF(m_object); } - - - virtual - ~ScriptInterpreterPythonObject() + + explicit operator bool() { return m_object && m_object != Py_None; } + + virtual ~ScriptInterpreterPythonObject() { if (Py_IsInitialized()) Py_XDECREF(m_object); m_object = NULL; } - private: - DISALLOW_COPY_AND_ASSIGN (ScriptInterpreterPythonObject); + + private: + DISALLOW_COPY_AND_ASSIGN(ScriptInterpreterPythonObject); }; -public: - class Locker : public ScriptInterpreterLocker - { - public: - + + public: + class Locker : public ScriptInterpreterLocker + { + public: enum OnEntry { - AcquireLock = 0x0001, - InitSession = 0x0002, - InitGlobals = 0x0004, - NoSTDIN = 0x0008 + AcquireLock = 0x0001, + InitSession = 0x0002, + InitGlobals = 0x0004, + NoSTDIN = 0x0008 }; - + enum OnLeave { - FreeLock = 0x0001, - FreeAcquiredLock = 0x0002, // do not free the lock if we already held it when calling constructor - TearDownSession = 0x0004 + FreeLock = 0x0001, + FreeAcquiredLock = 0x0002, // do not free the lock if we already held it when calling constructor + TearDownSession = 0x0004 }; - - Locker (ScriptInterpreterPython *py_interpreter = NULL, - uint16_t on_entry = AcquireLock | InitSession, - uint16_t on_leave = FreeLock | TearDownSession, - FILE *in = NULL, - FILE *out = NULL, - FILE *err = NULL); - - ~Locker (); - - private: - - bool - DoAcquireLock (); - - bool - DoInitSession (uint16_t on_entry_flags, FILE *in, FILE *out, FILE *err); - - bool - DoFreeLock (); - - bool - DoTearDownSession (); - - static void - ReleasePythonLock (); - - bool m_teardown_session; - ScriptInterpreterPython *m_python_interpreter; -// FILE* m_tmp_fh; - PyGILState_STATE m_GILState; - }; -protected: + Locker(ScriptInterpreterPython *py_interpreter = NULL, uint16_t on_entry = AcquireLock | InitSession, + uint16_t on_leave = FreeLock | TearDownSession, FILE *in = NULL, FILE *out = NULL, FILE *err = NULL); + + ~Locker(); + + private: + bool DoAcquireLock(); + + bool DoInitSession(uint16_t on_entry_flags, FILE *in, FILE *out, FILE *err); + + bool DoFreeLock(); + + bool DoTearDownSession(); + + static void ReleasePythonLock(); + + bool m_teardown_session; + ScriptInterpreterPython *m_python_interpreter; + // FILE* m_tmp_fh; + PyGILState_STATE m_GILState; + }; + + protected: uint32_t - IsExecutingPython () const + IsExecutingPython() const { return m_lock_count > 0; } @@ -474,23 +317,20 @@ return m_lock_count; } - enum ActiveIOHandler { + enum ActiveIOHandler + { eIOHandlerNone, eIOHandlerBreakpoint, eIOHandlerWatchpoint }; - PythonObject & - GetMainModule (); - - PythonDictionary & - GetSessionDictionary (); - - PythonDictionary & - GetSysModuleDictionary (); + PythonObject &GetMainModule(); + + PythonDictionary &GetSessionDictionary(); + + PythonDictionary &GetSysModuleDictionary(); + + bool GetEmbeddedInterpreterModuleObjects(); - bool - GetEmbeddedInterpreterModuleObjects (); - PythonObject m_saved_stdin; PythonObject m_saved_stdout; PythonObject m_saved_stderr; Index: include/lldb/ScriptInterpreter/ScriptInterpreter.h =================================================================== --- include/lldb/ScriptInterpreter/ScriptInterpreter.h +++ include/lldb/ScriptInterpreter/ScriptInterpreter.h @@ -17,152 +17,64 @@ #include "lldb/Utility/PseudoTerminal.h" - -namespace lldb_private { +namespace lldb_private +{ class ScriptInterpreterObject { -public: - ScriptInterpreterObject() : - m_object(NULL) - {} - - ScriptInterpreterObject(void* obj) : - m_object(obj) - {} - - ScriptInterpreterObject(const ScriptInterpreterObject& rhs) - : m_object(rhs.m_object) - {} - - virtual void* - GetObject() + public: + ScriptInterpreterObject() + : m_object(NULL) + { + } + + ScriptInterpreterObject(void *obj) + : m_object(obj) + { + } + + ScriptInterpreterObject(const ScriptInterpreterObject &rhs) + : m_object(rhs.m_object) + { + } + + virtual void *GetObject() { return m_object; } - + explicit operator bool () { return m_object != NULL; } - - ScriptInterpreterObject& - operator = (const ScriptInterpreterObject& rhs) + + ScriptInterpreterObject &operator=(const ScriptInterpreterObject &rhs) { if (this != &rhs) m_object = rhs.m_object; return *this; } - - virtual - ~ScriptInterpreterObject() - {} - -protected: - void* m_object; + + virtual ~ScriptInterpreterObject() {} + + protected: + void *m_object; }; - + class ScriptInterpreterLocker { -public: - - ScriptInterpreterLocker () - { - } - - virtual ~ScriptInterpreterLocker () - { - } -private: - DISALLOW_COPY_AND_ASSIGN (ScriptInterpreterLocker); -}; + public: + ScriptInterpreterLocker() {} + virtual ~ScriptInterpreterLocker() {} + + private: + DISALLOW_COPY_AND_ASSIGN(ScriptInterpreterLocker); +}; class ScriptInterpreter { -public: - - typedef void (*SWIGInitCallback) (void); - - typedef bool (*SWIGBreakpointCallbackFunction) (const char *python_function_name, - const char *session_dictionary_name, - const lldb::StackFrameSP& frame_sp, - const lldb::BreakpointLocationSP &bp_loc_sp); - - typedef bool (*SWIGWatchpointCallbackFunction) (const char *python_function_name, - const char *session_dictionary_name, - const lldb::StackFrameSP& frame_sp, - const lldb::WatchpointSP &wp_sp); - - typedef bool (*SWIGPythonTypeScriptCallbackFunction) (const char *python_function_name, - void *session_dictionary, - const lldb::ValueObjectSP& valobj_sp, - void** pyfunct_wrapper, - const lldb::TypeSummaryOptionsSP& options, - std::string& retval); - - typedef void* (*SWIGPythonCreateSyntheticProvider) (const char *python_class_name, - const char *session_dictionary_name, - const lldb::ValueObjectSP& valobj_sp); - - typedef void* (*SWIGPythonCreateScriptedThreadPlan) (const char *python_class_name, - const char *session_dictionary_name, - const lldb::ThreadPlanSP& thread_plan_sp); - - typedef bool (*SWIGPythonCallThreadPlan) (void *implementor, const char *method_name, Event *event_sp, bool &got_error); - - typedef void* (*SWIGPythonCreateOSPlugin) (const char *python_class_name, - const char *session_dictionary_name, - const lldb::ProcessSP& process_sp); - - typedef uint32_t (*SWIGPythonCalculateNumChildren) (void *implementor); - typedef void* (*SWIGPythonGetChildAtIndex) (void *implementor, uint32_t idx); - typedef int (*SWIGPythonGetIndexOfChildWithName) (void *implementor, const char* child_name); - typedef void* (*SWIGPythonCastPyObjectToSBValue) (void* data); - typedef lldb::ValueObjectSP (*SWIGPythonGetValueObjectSPFromSBValue) (void* data); - typedef bool (*SWIGPythonUpdateSynthProviderInstance) (void* data); - typedef bool (*SWIGPythonMightHaveChildrenSynthProviderInstance) (void* data); - typedef void* (*SWIGPythonGetValueSynthProviderInstance) (void *implementor); - - typedef bool (*SWIGPythonCallCommand) (const char *python_function_name, - const char *session_dictionary_name, - lldb::DebuggerSP& debugger, - const char* args, - lldb_private::CommandReturnObject& cmd_retobj, - lldb::ExecutionContextRefSP exe_ctx_ref_sp); - - typedef bool (*SWIGPythonCallModuleInit) (const char *python_module_name, - const char *session_dictionary_name, - lldb::DebuggerSP& debugger); - - typedef bool (*SWIGPythonScriptKeyword_Process) (const char* python_function_name, - const char* session_dictionary_name, - lldb::ProcessSP& process, - std::string& output); - typedef bool (*SWIGPythonScriptKeyword_Thread) (const char* python_function_name, - const char* session_dictionary_name, - lldb::ThreadSP& thread, - std::string& output); - - typedef bool (*SWIGPythonScriptKeyword_Target) (const char* python_function_name, - const char* session_dictionary_name, - lldb::TargetSP& target, - std::string& output); - - typedef bool (*SWIGPythonScriptKeyword_Frame) (const char* python_function_name, - const char* session_dictionary_name, - lldb::StackFrameSP& frame, - std::string& output); - - typedef bool (*SWIGPythonScriptKeyword_Value) (const char* python_function_name, - const char* session_dictionary_name, - lldb::ValueObjectSP& value, - std::string& output); - - typedef void* (*SWIGPython_GetDynamicSetting) (void* module, - const char* setting, - const lldb::TargetSP& target_sp); - + public: typedef enum { eScriptReturnTypeCharPtr, @@ -181,61 +93,61 @@ eScriptReturnTypeCharStrOrNone, eScriptReturnTypeOpaqueObject } ScriptReturnType; - - ScriptInterpreter (CommandInterpreter &interpreter, lldb::ScriptLanguage script_lang); - virtual ~ScriptInterpreter (); + ScriptInterpreter(CommandInterpreter &interpreter, lldb::ScriptLanguage script_lang); + + virtual ~ScriptInterpreter(); struct ExecuteScriptOptions { - public: - ExecuteScriptOptions () : - m_enable_io(true), - m_set_lldb_globals(true), - m_maskout_errors(true) + public: + ExecuteScriptOptions() + : m_enable_io(true) + , m_set_lldb_globals(true) + , m_maskout_errors(true) { } - + bool - GetEnableIO () const + GetEnableIO() const { return m_enable_io; } - + bool - GetSetLLDBGlobals () const + GetSetLLDBGlobals() const { return m_set_lldb_globals; } - + bool - GetMaskoutErrors () const + GetMaskoutErrors() const { return m_maskout_errors; } - - ExecuteScriptOptions& - SetEnableIO (bool enable) + + ExecuteScriptOptions & + SetEnableIO(bool enable) { m_enable_io = enable; return *this; } - ExecuteScriptOptions& - SetSetLLDBGlobals (bool set) + ExecuteScriptOptions & + SetSetLLDBGlobals(bool set) { m_set_lldb_globals = set; return *this; } - ExecuteScriptOptions& - SetMaskoutErrors (bool maskout) + ExecuteScriptOptions & + SetMaskoutErrors(bool maskout) { m_maskout_errors = maskout; return *this; } - - private: + + private: bool m_enable_io; bool m_set_lldb_globals; bool m_maskout_errors; @@ -247,26 +159,20 @@ return false; } - virtual bool - ExecuteOneLine (const char *command, - CommandReturnObject *result, - const ExecuteScriptOptions &options = ExecuteScriptOptions()) = 0; + virtual bool ExecuteOneLine(const char *command, CommandReturnObject *result, + const ExecuteScriptOptions &options = ExecuteScriptOptions()) = 0; - virtual void - ExecuteInterpreterLoop () = 0; + virtual void ExecuteInterpreterLoop() = 0; virtual bool - ExecuteOneLineWithReturn (const char *in_string, - ScriptReturnType return_type, - void *ret_value, - const ExecuteScriptOptions &options = ExecuteScriptOptions()) + ExecuteOneLineWithReturn(const char *in_string, ScriptReturnType return_type, void *ret_value, + const ExecuteScriptOptions &options = ExecuteScriptOptions()) { return true; } virtual Error - ExecuteMultipleLines (const char *in_string, - const ExecuteScriptOptions &options = ExecuteScriptOptions()) + ExecuteMultipleLines(const char *in_string, const ExecuteScriptOptions &options = ExecuteScriptOptions()) { Error error; error.SetErrorString("not implemented"); @@ -274,7 +180,7 @@ } virtual Error - ExportFunctionDefinitionToInterpreter (StringList &function_def) + ExportFunctionDefinitionToInterpreter(StringList &function_def) { Error error; error.SetErrorString("not implemented"); @@ -282,135 +188,121 @@ } virtual Error - GenerateBreakpointCommandCallbackData (StringList &input, std::string& output) + GenerateBreakpointCommandCallbackData(StringList &input, std::string &output) { Error error; error.SetErrorString("not implemented"); return error; } - + virtual bool - GenerateWatchpointCommandCallbackData (StringList &input, std::string& output) + GenerateWatchpointCommandCallbackData(StringList &input, std::string &output) { return false; } - + virtual bool - GenerateTypeScriptFunction (const char* oneliner, std::string& output, const void* name_token = NULL) + GenerateTypeScriptFunction(const char *oneliner, std::string &output, const void *name_token = NULL) { return false; } - + virtual bool - GenerateTypeScriptFunction (StringList &input, std::string& output, const void* name_token = NULL) + GenerateTypeScriptFunction(StringList &input, std::string &output, const void *name_token = NULL) { return false; } - + virtual bool - GenerateScriptAliasFunction (StringList &input, std::string& output) + GenerateScriptAliasFunction(StringList &input, std::string &output) { return false; } - + virtual bool - GenerateTypeSynthClass (StringList &input, std::string& output, const void* name_token = NULL) + GenerateTypeSynthClass(StringList &input, std::string &output, const void *name_token = NULL) { return false; } - + virtual bool - GenerateTypeSynthClass (const char* oneliner, std::string& output, const void* name_token = NULL) + GenerateTypeSynthClass(const char *oneliner, std::string &output, const void *name_token = NULL) { return false; } - + virtual lldb::ScriptInterpreterObjectSP - CreateSyntheticScriptedProvider (const char *class_name, - lldb::ValueObjectSP valobj) + CreateSyntheticScriptedProvider(const char *class_name, lldb::ValueObjectSP valobj) { return lldb::ScriptInterpreterObjectSP(); } - + virtual lldb::ScriptInterpreterObjectSP - OSPlugin_CreatePluginObject (const char *class_name, - lldb::ProcessSP process_sp) + OSPlugin_CreatePluginObject(const char *class_name, lldb::ProcessSP process_sp) { return lldb::ScriptInterpreterObjectSP(); } - + virtual lldb::ScriptInterpreterObjectSP - OSPlugin_RegisterInfo (lldb::ScriptInterpreterObjectSP os_plugin_object_sp) + OSPlugin_RegisterInfo(lldb::ScriptInterpreterObjectSP os_plugin_object_sp) { return lldb::ScriptInterpreterObjectSP(); } - + virtual lldb::ScriptInterpreterObjectSP - OSPlugin_ThreadsInfo (lldb::ScriptInterpreterObjectSP os_plugin_object_sp) + OSPlugin_ThreadsInfo(lldb::ScriptInterpreterObjectSP os_plugin_object_sp) { return lldb::ScriptInterpreterObjectSP(); } - + virtual lldb::ScriptInterpreterObjectSP - OSPlugin_RegisterContextData (lldb::ScriptInterpreterObjectSP os_plugin_object_sp, - lldb::tid_t thread_id) + OSPlugin_RegisterContextData(lldb::ScriptInterpreterObjectSP os_plugin_object_sp, lldb::tid_t thread_id) { return lldb::ScriptInterpreterObjectSP(); } virtual lldb::ScriptInterpreterObjectSP - OSPlugin_CreateThread (lldb::ScriptInterpreterObjectSP os_plugin_object_sp, - lldb::tid_t tid, - lldb::addr_t context) + OSPlugin_CreateThread(lldb::ScriptInterpreterObjectSP os_plugin_object_sp, lldb::tid_t tid, lldb::addr_t context) { return lldb::ScriptInterpreterObjectSP(); } - + virtual lldb::ScriptInterpreterObjectSP - CreateScriptedThreadPlan (const char *class_name, - lldb::ThreadPlanSP thread_plan_sp) + CreateScriptedThreadPlan(const char *class_name, lldb::ThreadPlanSP thread_plan_sp) { return lldb::ScriptInterpreterObjectSP(); } virtual bool - ScriptedThreadPlanExplainsStop (lldb::ScriptInterpreterObjectSP implementor_sp, - Event *event, - bool &script_error) + ScriptedThreadPlanExplainsStop(lldb::ScriptInterpreterObjectSP implementor_sp, Event *event, bool &script_error) { script_error = true; return true; } virtual bool - ScriptedThreadPlanShouldStop (lldb::ScriptInterpreterObjectSP implementor_sp, - Event *event, - bool &script_error) + ScriptedThreadPlanShouldStop(lldb::ScriptInterpreterObjectSP implementor_sp, Event *event, bool &script_error) { script_error = true; return true; } virtual lldb::StateType - ScriptedThreadPlanGetRunState (lldb::ScriptInterpreterObjectSP implementor_sp, - bool &script_error) + ScriptedThreadPlanGetRunState(lldb::ScriptInterpreterObjectSP implementor_sp, bool &script_error) { script_error = true; return lldb::eStateStepping; } virtual lldb::ScriptInterpreterObjectSP - LoadPluginModule (const FileSpec& file_spec, - lldb_private::Error& error) + LoadPluginModule(const FileSpec &file_spec, lldb_private::Error &error) { return lldb::ScriptInterpreterObjectSP(); } - + virtual lldb::ScriptInterpreterObjectSP - GetDynamicSettings (lldb::ScriptInterpreterObjectSP plugin_module_sp, - Target* target, - const char* setting_name, - lldb_private::Error& error) + GetDynamicSettings(lldb::ScriptInterpreterObjectSP plugin_module_sp, Target *target, const char *setting_name, + lldb_private::Error &error) { return lldb::ScriptInterpreterObjectSP(); } @@ -423,185 +315,149 @@ return error; } - virtual void - CollectDataForBreakpointCommandCallback (std::vector &options, - CommandReturnObject &result); + virtual void CollectDataForBreakpointCommandCallback(std::vector &options, CommandReturnObject &result); - virtual void - CollectDataForWatchpointCommandCallback (WatchpointOptions *wp_options, - CommandReturnObject &result); + virtual void CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options, CommandReturnObject &result); /// Set the specified text as the callback for the breakpoint. - Error - SetBreakpointCommandCallback (std::vector &bp_options_vec, - const char *callback_text); + Error SetBreakpointCommandCallback(std::vector &bp_options_vec, const char *callback_text); virtual Error - SetBreakpointCommandCallback (BreakpointOptions *bp_options, - const char *callback_text) + SetBreakpointCommandCallback(BreakpointOptions *bp_options, const char *callback_text) { Error error; error.SetErrorString("unimplemented"); return error; } - - void - SetBreakpointCommandCallbackFunction (std::vector &bp_options_vec, - const char *function_name); + + void SetBreakpointCommandCallbackFunction(std::vector &bp_options_vec, const char *function_name); /// Set a one-liner as the callback for the breakpoint. - virtual void - SetBreakpointCommandCallbackFunction (BreakpointOptions *bp_options, - const char *function_name) + virtual void + SetBreakpointCommandCallbackFunction(BreakpointOptions *bp_options, const char *function_name) { return; } - + /// Set a one-liner as the callback for the watchpoint. - virtual void - SetWatchpointCommandCallback (WatchpointOptions *wp_options, - const char *oneliner) + virtual void + SetWatchpointCommandCallback(WatchpointOptions *wp_options, const char *oneliner) { return; } - + virtual bool - GetScriptedSummary (const char *function_name, - lldb::ValueObjectSP valobj, - lldb::ScriptInterpreterObjectSP& callee_wrapper_sp, - const TypeSummaryOptions& options, - std::string& retval) + GetScriptedSummary(const char *function_name, lldb::ValueObjectSP valobj, lldb::ScriptInterpreterObjectSP &callee_wrapper_sp, + const TypeSummaryOptions &options, std::string &retval) { return false; } - + virtual void - Clear () + Clear() { // Clean up any ref counts to SBObjects that might be in global variables } - + virtual size_t - CalculateNumChildren (const lldb::ScriptInterpreterObjectSP& implementor) + CalculateNumChildren(const lldb::ScriptInterpreterObjectSP &implementor) { return 0; } - + virtual lldb::ValueObjectSP - GetChildAtIndex (const lldb::ScriptInterpreterObjectSP& implementor, uint32_t idx) + GetChildAtIndex(const lldb::ScriptInterpreterObjectSP &implementor, uint32_t idx) { return lldb::ValueObjectSP(); } - + virtual int - GetIndexOfChildWithName (const lldb::ScriptInterpreterObjectSP& implementor, const char* child_name) + GetIndexOfChildWithName(const lldb::ScriptInterpreterObjectSP &implementor, const char *child_name) { return UINT32_MAX; } - + virtual bool - UpdateSynthProviderInstance (const lldb::ScriptInterpreterObjectSP& implementor) + UpdateSynthProviderInstance(const lldb::ScriptInterpreterObjectSP &implementor) { return false; } - + virtual bool - MightHaveChildrenSynthProviderInstance (const lldb::ScriptInterpreterObjectSP& implementor) + MightHaveChildrenSynthProviderInstance(const lldb::ScriptInterpreterObjectSP &implementor) { return true; } - + virtual lldb::ValueObjectSP - GetSyntheticValue (const lldb::ScriptInterpreterObjectSP& implementor) + GetSyntheticValue(const lldb::ScriptInterpreterObjectSP &implementor) { return nullptr; } - + virtual bool - RunScriptBasedCommand (const char* impl_function, - const char* args, - ScriptedCommandSynchronicity synchronicity, - lldb_private::CommandReturnObject& cmd_retobj, - Error& error, - const lldb_private::ExecutionContext& exe_ctx) + RunScriptBasedCommand(const char *impl_function, const char *args, ScriptedCommandSynchronicity synchronicity, + lldb_private::CommandReturnObject &cmd_retobj, Error &error, const lldb_private::ExecutionContext &exe_ctx) { return false; } - + virtual bool - RunScriptFormatKeyword (const char* impl_function, - Process* process, - std::string& output, - Error& error) + RunScriptFormatKeyword(const char *impl_function, Process *process, std::string &output, Error &error) { error.SetErrorString("unimplemented"); return false; } virtual bool - RunScriptFormatKeyword (const char* impl_function, - Thread* thread, - std::string& output, - Error& error) + RunScriptFormatKeyword(const char *impl_function, Thread *thread, std::string &output, Error &error) { error.SetErrorString("unimplemented"); return false; } - + virtual bool - RunScriptFormatKeyword (const char* impl_function, - Target* target, - std::string& output, - Error& error) + RunScriptFormatKeyword(const char *impl_function, Target *target, std::string &output, Error &error) { error.SetErrorString("unimplemented"); return false; } - + virtual bool - RunScriptFormatKeyword (const char* impl_function, - StackFrame* frame, - std::string& output, - Error& error) + RunScriptFormatKeyword(const char *impl_function, StackFrame *frame, std::string &output, Error &error) { error.SetErrorString("unimplemented"); return false; } - + virtual bool - RunScriptFormatKeyword (const char* impl_function, - ValueObject* value, - std::string& output, - Error& error) + RunScriptFormatKeyword(const char *impl_function, ValueObject *value, std::string &output, Error &error) { error.SetErrorString("unimplemented"); return false; } - + virtual bool - GetDocumentationForItem (const char* item, std::string& dest) + GetDocumentationForItem(const char *item, std::string &dest) { - dest.clear(); + dest.clear(); return false; } - + virtual bool - CheckObjectExists (const char* name) + CheckObjectExists(const char *name) { return false; } virtual bool - LoadScriptingModule (const char* filename, - bool can_reload, - bool init_session, - lldb_private::Error& error, - lldb::ScriptInterpreterObjectSP* module_sp = nullptr) + LoadScriptingModule(const char *filename, bool can_reload, bool init_session, lldb_private::Error &error, + lldb::ScriptInterpreterObjectSP *module_sp = nullptr) { error.SetErrorString("loading unimplemented"); return false; } - + virtual bool IsReservedWord (const char* word) { @@ -609,56 +465,32 @@ } virtual lldb::ScriptInterpreterObjectSP - MakeScriptObject (void* object) + MakeScriptObject(void *object) { return lldb::ScriptInterpreterObjectSP(new ScriptInterpreterObject(object)); } - - virtual std::unique_ptr - AcquireInterpreterLock (); - - const char * - GetScriptInterpreterPtyName (); - - int - GetMasterFileDescriptor (); - - CommandInterpreter & - GetCommandInterpreter (); - - static std::string - LanguageToString (lldb::ScriptLanguage language); - - static void - InitializeInterpreter (SWIGInitCallback python_swig_init_callback, - SWIGBreakpointCallbackFunction swig_breakpoint_callback, - SWIGWatchpointCallbackFunction swig_watchpoint_callback, - SWIGPythonTypeScriptCallbackFunction swig_typescript_callback, - SWIGPythonCreateSyntheticProvider swig_synthetic_script, - SWIGPythonCalculateNumChildren swig_calc_children, - SWIGPythonGetChildAtIndex swig_get_child_index, - SWIGPythonGetIndexOfChildWithName swig_get_index_child, - SWIGPythonCastPyObjectToSBValue swig_cast_to_sbvalue , - SWIGPythonGetValueObjectSPFromSBValue swig_get_valobj_sp_from_sbvalue, - SWIGPythonUpdateSynthProviderInstance swig_update_provider, - SWIGPythonMightHaveChildrenSynthProviderInstance swig_mighthavechildren_provider, - SWIGPythonGetValueSynthProviderInstance swig_getvalue_provider, - SWIGPythonCallCommand swig_call_command, - SWIGPythonCallModuleInit swig_call_module_init, - SWIGPythonCreateOSPlugin swig_create_os_plugin, - SWIGPythonScriptKeyword_Process swig_run_script_keyword_process, - SWIGPythonScriptKeyword_Thread swig_run_script_keyword_thread, - SWIGPythonScriptKeyword_Target swig_run_script_keyword_target, - SWIGPythonScriptKeyword_Frame swig_run_script_keyword_frame, - SWIGPythonScriptKeyword_Value swig_run_script_keyword_value, - SWIGPython_GetDynamicSetting swig_plugin_get, - SWIGPythonCreateScriptedThreadPlan swig_thread_plan_script, - SWIGPythonCallThreadPlan swig_call_thread_plan); + + virtual std::unique_ptr AcquireInterpreterLock(); + + const char *GetScriptInterpreterPtyName(); + + int GetMasterFileDescriptor(); + + CommandInterpreter &GetCommandInterpreter(); + + static std::string LanguageToString(lldb::ScriptLanguage language); virtual void - ResetOutputFileHandle (FILE *new_fh) { } //By default, do nothing. + ResetOutputFileHandle(FILE *new_fh) + { + } // By default, do nothing. + + static ScriptInterpreter *GetScriptInterpreter(lldb::ScriptLanguage script_lang, CommandInterpreter &command_interpreter, + bool can_create); + + static void InitializePrivate(); -protected: + protected: CommandInterpreter &m_interpreter; lldb::ScriptLanguage m_script_lang; }; Index: include/lldb/ScriptInterpreter/ScriptInterpreterNone.h =================================================================== --- include/lldb/ScriptInterpreter/ScriptInterpreterNone.h +++ include/lldb/ScriptInterpreter/ScriptInterpreterNone.h @@ -10,24 +10,21 @@ #ifndef liblldb_ScriptInterpreterNone_h_ #define liblldb_ScriptInterpreterNone_h_ -#include "lldb/Interpreter/ScriptInterpreter.h" +#include "lldb/ScriptInterpreter/ScriptInterpreter.h" -namespace lldb_private { +namespace lldb_private +{ class ScriptInterpreterNone : public ScriptInterpreter { -public: - - ScriptInterpreterNone (CommandInterpreter &interpreter); - - ~ScriptInterpreterNone (); + public: + ScriptInterpreterNone(CommandInterpreter &interpreter); - bool - ExecuteOneLine (const char *command, CommandReturnObject *result, const ExecuteScriptOptions &options = ExecuteScriptOptions()); + ~ScriptInterpreterNone(); - void - ExecuteInterpreterLoop (); + bool ExecuteOneLine(const char *command, CommandReturnObject *result, const ExecuteScriptOptions &options = ExecuteScriptOptions()); + void ExecuteInterpreterLoop(); }; } // namespace lldb_private Index: include/lldb/lldb-forward.h =================================================================== --- include/lldb/lldb-forward.h +++ include/lldb/lldb-forward.h @@ -184,7 +184,6 @@ class ScriptInterpreterLocker; class ScriptInterpreterObject; #ifndef LLDB_DISABLE_PYTHON -class ScriptInterpreterPython; struct ScriptSummaryFormat; #endif class SearchFilter; @@ -364,6 +363,7 @@ typedef std::shared_ptr QueueSP; typedef std::weak_ptr QueueWP; typedef std::shared_ptr QueueItemSP; + typedef std::shared_ptr ScriptInterpreterUP; typedef std::shared_ptr ScriptInterpreterObjectSP; #ifndef LLDB_DISABLE_PYTHON typedef std::shared_ptr ScriptSummaryFormatSP; Index: source/API/SBBreakpoint.cpp =================================================================== --- source/API/SBBreakpoint.cpp +++ source/API/SBBreakpoint.cpp @@ -25,7 +25,7 @@ #include "lldb/Core/Stream.h" #include "lldb/Core/StreamFile.h" #include "lldb/Interpreter/CommandInterpreter.h" -#include "lldb/Interpreter/ScriptInterpreter.h" +#include "lldb/ScriptInterpreter/ScriptInterpreter.h" #include "lldb/Target/Process.h" #include "lldb/Target/SectionLoadList.h" #include "lldb/Target/Target.h" Index: source/API/SBBreakpointLocation.cpp =================================================================== --- source/API/SBBreakpointLocation.cpp +++ source/API/SBBreakpointLocation.cpp @@ -22,7 +22,7 @@ #include "lldb/Core/Stream.h" #include "lldb/Core/StreamFile.h" #include "lldb/Interpreter/CommandInterpreter.h" -#include "lldb/Interpreter/ScriptInterpreter.h" +#include "lldb/ScriptInterpreter/ScriptInterpreter.h" #include "lldb/Target/ThreadSpec.h" #include "lldb/Target/Target.h" #include "lldb/Target/ThreadSpec.h" Index: source/API/SBCommandInterpreter.cpp =================================================================== --- source/API/SBCommandInterpreter.cpp +++ source/API/SBCommandInterpreter.cpp @@ -566,170 +566,6 @@ return false; } -#ifndef LLDB_DISABLE_PYTHON - -// Defined in the SWIG source file -extern "C" void -init_lldb(void); - -// these are the Pythonic implementations of the required callbacks -// these are scripting-language specific, which is why they belong here -// we still need to use function pointers to them instead of relying -// on linkage-time resolution because the SWIG stuff and this file -// get built at different times -extern "C" bool -LLDBSwigPythonBreakpointCallbackFunction (const char *python_function_name, - const char *session_dictionary_name, - const lldb::StackFrameSP& sb_frame, - const lldb::BreakpointLocationSP& sb_bp_loc); - -extern "C" bool -LLDBSwigPythonWatchpointCallbackFunction (const char *python_function_name, - const char *session_dictionary_name, - const lldb::StackFrameSP& sb_frame, - const lldb::WatchpointSP& sb_wp); - -extern "C" bool -LLDBSwigPythonCallTypeScript (const char *python_function_name, - void *session_dictionary, - const lldb::ValueObjectSP& valobj_sp, - void** pyfunct_wrapper, - const lldb::TypeSummaryOptionsSP& options_sp, - std::string& retval); - -extern "C" void* -LLDBSwigPythonCreateSyntheticProvider (const char *python_class_name, - const char *session_dictionary_name, - const lldb::ValueObjectSP& valobj_sp); - - -extern "C" void* -LLDBSwigPythonCreateScriptedThreadPlan (const char *python_class_name, - const char *session_dictionary_name, - const lldb::ThreadPlanSP& thread_plan_sp); - -extern "C" bool -LLDBSWIGPythonCallThreadPlan (void *implementor, - const char *method_name, - Event *event_sp, - bool &got_error); - -extern "C" uint32_t -LLDBSwigPython_CalculateNumChildren (void *implementor); - -extern "C" void * -LLDBSwigPython_GetChildAtIndex (void *implementor, uint32_t idx); - -extern "C" int -LLDBSwigPython_GetIndexOfChildWithName (void *implementor, const char* child_name); - -extern "C" void * -LLDBSWIGPython_CastPyObjectToSBValue (void* data); - -extern lldb::ValueObjectSP -LLDBSWIGPython_GetValueObjectSPFromSBValue (void* data); - -extern "C" bool -LLDBSwigPython_UpdateSynthProviderInstance (void* implementor); - -extern "C" bool -LLDBSwigPython_MightHaveChildrenSynthProviderInstance (void* implementor); - -extern "C" void * -LLDBSwigPython_GetValueSynthProviderInstance (void* implementor); - -extern "C" bool -LLDBSwigPythonCallCommand (const char *python_function_name, - const char *session_dictionary_name, - lldb::DebuggerSP& debugger, - const char* args, - lldb_private::CommandReturnObject &cmd_retobj, - lldb::ExecutionContextRefSP exe_ctx_ref_sp); - -extern "C" bool -LLDBSwigPythonCallModuleInit (const char *python_module_name, - const char *session_dictionary_name, - lldb::DebuggerSP& debugger); - -extern "C" void* -LLDBSWIGPythonCreateOSPlugin (const char *python_class_name, - const char *session_dictionary_name, - const lldb::ProcessSP& process_sp); - -extern "C" bool -LLDBSWIGPythonRunScriptKeywordProcess (const char* python_function_name, - const char* session_dictionary_name, - lldb::ProcessSP& process, - std::string& output); - -extern "C" bool -LLDBSWIGPythonRunScriptKeywordThread (const char* python_function_name, - const char* session_dictionary_name, - lldb::ThreadSP& thread, - std::string& output); - -extern "C" bool -LLDBSWIGPythonRunScriptKeywordTarget (const char* python_function_name, - const char* session_dictionary_name, - lldb::TargetSP& target, - std::string& output); - -extern "C" bool -LLDBSWIGPythonRunScriptKeywordFrame (const char* python_function_name, - const char* session_dictionary_name, - lldb::StackFrameSP& frame, - std::string& output); - -extern "C" bool -LLDBSWIGPythonRunScriptKeywordValue (const char* python_function_name, - const char* session_dictionary_name, - lldb::ValueObjectSP& value, - std::string& output); - -extern "C" void* -LLDBSWIGPython_GetDynamicSetting (void* module, - const char* setting, - const lldb::TargetSP& target_sp); - - -#endif - -void -SBCommandInterpreter::InitializeSWIG () -{ - static bool g_initialized = false; - if (!g_initialized) - { - g_initialized = true; -#ifndef LLDB_DISABLE_PYTHON - ScriptInterpreter::InitializeInterpreter (init_lldb, - LLDBSwigPythonBreakpointCallbackFunction, - LLDBSwigPythonWatchpointCallbackFunction, - LLDBSwigPythonCallTypeScript, - LLDBSwigPythonCreateSyntheticProvider, - LLDBSwigPython_CalculateNumChildren, - LLDBSwigPython_GetChildAtIndex, - LLDBSwigPython_GetIndexOfChildWithName, - LLDBSWIGPython_CastPyObjectToSBValue, - LLDBSWIGPython_GetValueObjectSPFromSBValue, - LLDBSwigPython_UpdateSynthProviderInstance, - LLDBSwigPython_MightHaveChildrenSynthProviderInstance, - LLDBSwigPython_GetValueSynthProviderInstance, - LLDBSwigPythonCallCommand, - LLDBSwigPythonCallModuleInit, - LLDBSWIGPythonCreateOSPlugin, - LLDBSWIGPythonRunScriptKeywordProcess, - LLDBSWIGPythonRunScriptKeywordThread, - LLDBSWIGPythonRunScriptKeywordTarget, - LLDBSWIGPythonRunScriptKeywordFrame, - LLDBSWIGPythonRunScriptKeywordValue, - LLDBSWIGPython_GetDynamicSetting, - LLDBSwigPythonCreateScriptedThreadPlan, - LLDBSWIGPythonCallThreadPlan); -#endif - } -} - lldb::SBCommand SBCommandInterpreter::AddMultiwordCommand (const char* name, const char* help) { Index: source/API/SBDebugger.cpp =================================================================== --- source/API/SBDebugger.cpp +++ source/API/SBDebugger.cpp @@ -115,8 +115,6 @@ if (log) log->Printf ("SBDebugger::Initialize ()"); - SBCommandInterpreter::InitializeSWIG (); - Debugger::Initialize(LoadPlugin); } Index: source/API/SBTypeCategory.cpp =================================================================== --- source/API/SBTypeCategory.cpp +++ source/API/SBTypeCategory.cpp @@ -21,7 +21,7 @@ #include "lldb/Core/Debugger.h" #include "lldb/DataFormatters/DataVisualization.h" #include "lldb/Interpreter/CommandInterpreter.h" -#include "lldb/Interpreter/ScriptInterpreter.h" +#include "lldb/ScriptInterpreter/ScriptInterpreter.h" using namespace lldb; using namespace lldb_private; Index: source/CMakeLists.txt =================================================================== --- source/CMakeLists.txt +++ source/CMakeLists.txt @@ -34,6 +34,7 @@ add_subdirectory(Host) add_subdirectory(Interpreter) add_subdirectory(Plugins) +add_subdirectory(ScriptInterpreter) add_subdirectory(Symbol) add_subdirectory(Target) add_subdirectory(Utility) Index: source/Commands/CommandObjectCommands.cpp =================================================================== --- source/Commands/CommandObjectCommands.cpp +++ source/Commands/CommandObjectCommands.cpp @@ -28,8 +28,7 @@ #include "lldb/Interpreter/OptionValueBoolean.h" #include "lldb/Interpreter/OptionValueUInt64.h" #include "lldb/Interpreter/Options.h" -#include "lldb/Interpreter/ScriptInterpreter.h" -#include "lldb/Interpreter/ScriptInterpreterPython.h" +#include "lldb/ScriptInterpreter/ScriptInterpreter.h" using namespace lldb; using namespace lldb_private; Index: source/Core/Module.cpp =================================================================== --- source/Core/Module.cpp +++ source/Core/Module.cpp @@ -24,7 +24,7 @@ #include "lldb/Host/Host.h" #include "lldb/Host/Symbols.h" #include "lldb/Interpreter/CommandInterpreter.h" -#include "lldb/Interpreter/ScriptInterpreter.h" +#include "lldb/ScriptInterpreter/ScriptInterpreter.h" #include "lldb/lldb-private-log.h" #include "lldb/Symbol/CompileUnit.h" #include "lldb/Symbol/ObjectFile.h" Index: source/Core/ValueObject.cpp =================================================================== --- source/Core/ValueObject.cpp +++ source/Core/ValueObject.cpp @@ -43,7 +43,6 @@ #include "lldb/Host/Endian.h" #include "lldb/Interpreter/CommandInterpreter.h" -#include "lldb/Interpreter/ScriptInterpreterPython.h" #include "lldb/Symbol/ClangASTType.h" #include "lldb/Symbol/ClangASTContext.h" Index: source/DataFormatters/FormatManager.cpp =================================================================== --- source/DataFormatters/FormatManager.cpp +++ source/DataFormatters/FormatManager.cpp @@ -18,7 +18,6 @@ #include "lldb/Core/Debugger.h" #include "lldb/DataFormatters/CXXFormatterFunctions.h" -#include "lldb/Interpreter/ScriptInterpreterPython.h" #include "lldb/Target/ExecutionContext.h" #include "lldb/Target/Platform.h" #include "llvm/ADT/STLExtras.h" Index: source/DataFormatters/TypeSynthetic.cpp =================================================================== --- source/DataFormatters/TypeSynthetic.cpp +++ source/DataFormatters/TypeSynthetic.cpp @@ -23,7 +23,9 @@ #include "lldb/Core/StreamString.h" #include "lldb/DataFormatters/TypeSynthetic.h" #include "lldb/Interpreter/CommandInterpreter.h" -#include "lldb/Interpreter/ScriptInterpreterPython.h" +#ifndef LLDB_DISABLE_PYTHON +#include "lldb/ScriptInterpreter/Python/ScriptInterpreterPython.h" +#endif #include "lldb/Symbol/ClangASTType.h" #include "lldb/Target/StackFrame.h" #include "lldb/Target/Target.h" Index: source/Interpreter/CMakeLists.txt =================================================================== --- source/Interpreter/CMakeLists.txt +++ source/Interpreter/CMakeLists.txt @@ -43,8 +43,4 @@ OptionGroupWatchpoint.cpp Options.cpp Property.cpp - PythonDataObjects.cpp - ScriptInterpreter.cpp - ScriptInterpreterNone.cpp - ScriptInterpreterPython.cpp ) Index: source/Interpreter/CommandInterpreter.cpp =================================================================== --- source/Interpreter/CommandInterpreter.cpp +++ source/Interpreter/CommandInterpreter.cpp @@ -60,8 +60,6 @@ #include "lldb/Interpreter/CommandInterpreter.h" #include "lldb/Interpreter/CommandReturnObject.h" #include "lldb/Interpreter/Options.h" -#include "lldb/Interpreter/ScriptInterpreterNone.h" -#include "lldb/Interpreter/ScriptInterpreterPython.h" #include "lldb/Target/Process.h" @@ -2850,42 +2848,8 @@ ScriptInterpreter * CommandInterpreter::GetScriptInterpreter (bool can_create) { - if (m_script_interpreter_ap.get() != nullptr) - return m_script_interpreter_ap.get(); - - if (!can_create) - return nullptr; - - // - // we need to protect the initialization of the script interpreter - // otherwise we could end up with two threads both trying to create - // their instance of it, and for some languages (e.g. Python) - // this is a bulletproof recipe for disaster! - // this needs to be a function-level static because multiple Debugger instances living in the same process - // still need to be isolated and not try to initialize Python concurrently - static Mutex g_interpreter_mutex(Mutex::eMutexTypeRecursive); - Mutex::Locker interpreter_lock(g_interpreter_mutex); - - Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); - if (log) - log->Printf("Initializing the ScriptInterpreter now\n"); - lldb::ScriptLanguage script_lang = GetDebugger().GetScriptLanguage(); - switch (script_lang) - { - case eScriptLanguagePython: -#ifndef LLDB_DISABLE_PYTHON - m_script_interpreter_ap.reset (new ScriptInterpreterPython (*this)); - break; -#else - // Fall through to the None case when python is disabled -#endif - case eScriptLanguageNone: - m_script_interpreter_ap.reset (new ScriptInterpreterNone (*this)); - break; - }; - - return m_script_interpreter_ap.get(); + return ScriptInterpreter::GetScriptInterpreter(script_lang, *this, can_create); } Index: source/Interpreter/CommandObject.cpp =================================================================== --- source/Interpreter/CommandObject.cpp +++ source/Interpreter/CommandObject.cpp @@ -30,8 +30,6 @@ #include "lldb/Interpreter/CommandInterpreter.h" #include "lldb/Interpreter/CommandReturnObject.h" -#include "lldb/Interpreter/ScriptInterpreter.h" -#include "lldb/Interpreter/ScriptInterpreterPython.h" using namespace lldb; using namespace lldb_private; Index: source/Interpreter/CommandObjectScript.cpp =================================================================== --- source/Interpreter/CommandObjectScript.cpp +++ source/Interpreter/CommandObjectScript.cpp @@ -23,7 +23,7 @@ #include "lldb/Interpreter/Args.h" #include "lldb/Interpreter/CommandInterpreter.h" #include "lldb/Interpreter/CommandReturnObject.h" -#include "lldb/Interpreter/ScriptInterpreter.h" +#include "lldb/ScriptInterpreter/ScriptInterpreter.h" using namespace lldb; using namespace lldb_private; Index: source/Plugins/OperatingSystem/Python/OperatingSystemPython.h =================================================================== --- source/Plugins/OperatingSystem/Python/OperatingSystemPython.h +++ source/Plugins/OperatingSystem/Python/OperatingSystemPython.h @@ -14,7 +14,7 @@ // C Includes // C++ Includes // Other libraries and framework includes -#include "lldb/Interpreter/ScriptInterpreter.h" +#include "lldb/ScriptInterpreter/ScriptInterpreter.h" #include "lldb/Target/OperatingSystem.h" class DynamicRegisterInfo; Index: source/Plugins/OperatingSystem/Python/OperatingSystemPython.cpp =================================================================== --- source/Plugins/OperatingSystem/Python/OperatingSystemPython.cpp +++ source/Plugins/OperatingSystem/Python/OperatingSystemPython.cpp @@ -24,7 +24,7 @@ #include "lldb/Core/StreamString.h" #include "lldb/Core/ValueObjectVariable.h" #include "lldb/Interpreter/CommandInterpreter.h" -#include "lldb/Interpreter/PythonDataObjects.h" +#include "lldb/ScriptInterpreter/Python/PythonDataObjects.h" #include "lldb/Symbol/ClangNamespaceDecl.h" #include "lldb/Symbol/ObjectFile.h" #include "lldb/Symbol/VariableList.h" Index: source/Plugins/Process/Utility/DynamicRegisterInfo.cpp =================================================================== --- source/Plugins/Process/Utility/DynamicRegisterInfo.cpp +++ source/Plugins/Process/Utility/DynamicRegisterInfo.cpp @@ -21,7 +21,7 @@ #include "lldb/DataFormatters/FormatManager.h" #ifndef LLDB_DISABLE_PYTHON -#include "lldb/Interpreter/PythonDataObjects.h" +#include "lldb/ScriptInterpreter/Python/PythonDataObjects.h" #endif using namespace lldb; Index: source/Plugins/Process/gdb-remote/GDBRemoteRegisterContext.cpp =================================================================== --- source/Plugins/Process/gdb-remote/GDBRemoteRegisterContext.cpp +++ source/Plugins/Process/gdb-remote/GDBRemoteRegisterContext.cpp @@ -17,9 +17,6 @@ #include "lldb/Core/RegisterValue.h" #include "lldb/Core/Scalar.h" #include "lldb/Core/StreamString.h" -#ifndef LLDB_DISABLE_PYTHON -#include "lldb/Interpreter/PythonDataObjects.h" -#endif #include "lldb/Target/ExecutionContext.h" #include "lldb/Target/Target.h" #include "lldb/Utility/Utils.h" Index: source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp =================================================================== --- source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp +++ source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp @@ -51,7 +51,7 @@ #include "lldb/Interpreter/CommandObjectMultiword.h" #include "lldb/Interpreter/CommandReturnObject.h" #ifndef LLDB_DISABLE_PYTHON -#include "lldb/Interpreter/PythonDataObjects.h" +#include "lldb/ScriptInterpreter/Python/PythonDataObjects.h" #endif #include "lldb/Symbol/ObjectFile.h" #include "lldb/Target/DynamicLoader.h" Index: source/ScriptInterpreter/CMakeLists.txt =================================================================== --- /dev/null +++ source/ScriptInterpreter/CMakeLists.txt @@ -0,0 +1,9 @@ + +add_lldb_library(lldbScriptInterpreter + ScriptInterpreter.cpp + ScriptInterpreterNone.cpp + ) + +if (NOT LLDB_DISABLE_PYTHON) + add_subdirectory(Python) +endif() Index: source/ScriptInterpreter/Python/CMakeLists.txt =================================================================== --- /dev/null +++ source/ScriptInterpreter/Python/CMakeLists.txt @@ -0,0 +1,6 @@ + +add_lldb_library(lldbPythonInterpreter + PythonDataObjects.cpp + ScriptInterpreterPython.cpp + ) + \ No newline at end of file Index: source/ScriptInterpreter/Python/PythonDataObjects.cpp =================================================================== --- source/ScriptInterpreter/Python/PythonDataObjects.cpp +++ source/ScriptInterpreter/Python/PythonDataObjects.cpp @@ -21,8 +21,8 @@ #include "lldb/Core/Stream.h" #include "lldb/Host/File.h" -#include "lldb/Interpreter/PythonDataObjects.h" -#include "lldb/Interpreter/ScriptInterpreter.h" +#include "lldb/ScriptInterpreter/Python/PythonDataObjects.h" +#include "lldb/ScriptInterpreter/ScriptInterpreter.h" using namespace lldb_private; using namespace lldb; @@ -30,62 +30,62 @@ //---------------------------------------------------------------------- // PythonObject //---------------------------------------------------------------------- -PythonObject::PythonObject (const lldb::ScriptInterpreterObjectSP &script_object_sp) : - m_py_obj (nullptr) +PythonObject::PythonObject(const lldb::ScriptInterpreterObjectSP &script_object_sp) + : m_py_obj(nullptr) { if (script_object_sp) - Reset ((PyObject *)script_object_sp->GetObject()); + Reset((PyObject *)script_object_sp->GetObject()); } void -PythonObject::Dump (Stream &strm) const +PythonObject::Dump(Stream &strm) const { if (m_py_obj) { FILE *file = ::tmpfile(); if (file) { - ::PyObject_Print (m_py_obj, file, 0); - const long length = ftell (file); + ::PyObject_Print(m_py_obj, file, 0); + const long length = ftell(file); if (length) { ::rewind(file); - std::vector file_contents (length,'\0'); - const size_t length_read = ::fread (file_contents.data(), 1, file_contents.size(), file); + std::vector file_contents(length, '\0'); + const size_t length_read = ::fread(file_contents.data(), 1, file_contents.size(), file); if (length_read > 0) - strm.Write (file_contents.data(), length_read); + strm.Write(file_contents.data(), length_read); } - ::fclose (file); + ::fclose(file); } } else - strm.PutCString ("NULL"); + strm.PutCString("NULL"); } PythonString -PythonObject::Repr () +PythonObject::Repr() { if (!m_py_obj) - return PythonString (); + return PythonString(); PyObject *repr = PyObject_Repr(m_py_obj); if (!repr) - return PythonString (); + return PythonString(); return PythonString(repr); } PythonString -PythonObject::Str () +PythonObject::Str() { if (!m_py_obj) - return PythonString (); + return PythonString(); PyObject *str = PyObject_Str(m_py_obj); if (!str) - return PythonString (); + return PythonString(); return PythonString(str); } bool -PythonObject::IsNULLOrNone () const +PythonObject::IsNULLOrNone() const { return ((m_py_obj == nullptr) || (m_py_obj == Py_None)); } @@ -94,50 +94,50 @@ // PythonString //---------------------------------------------------------------------- -PythonString::PythonString (PyObject *py_obj) : - PythonObject() +PythonString::PythonString(PyObject *py_obj) + : PythonObject() { Reset(py_obj); // Use "Reset()" to ensure that py_obj is a string } -PythonString::PythonString (const PythonObject &object) : - PythonObject() +PythonString::PythonString(const PythonObject &object) + : PythonObject() { Reset(object.get()); // Use "Reset()" to ensure that py_obj is a string } -PythonString::PythonString (const lldb::ScriptInterpreterObjectSP &script_object_sp) : - PythonObject() +PythonString::PythonString(const lldb::ScriptInterpreterObjectSP &script_object_sp) + : PythonObject() { if (script_object_sp) Reset((PyObject *)script_object_sp->GetObject()); // Use "Reset()" to ensure that py_obj is a string } -PythonString::PythonString (const char* string) : - PythonObject(PyString_FromString(string)) +PythonString::PythonString(const char *string) + : PythonObject(PyString_FromString(string)) { } -PythonString::PythonString () : - PythonObject() +PythonString::PythonString() + : PythonObject() { } -PythonString::~PythonString () +PythonString::~PythonString() { } bool -PythonString::Reset (PyObject *py_obj) +PythonString::Reset(PyObject *py_obj) { if (py_obj && PyString_Check(py_obj)) return PythonObject::Reset(py_obj); - + PythonObject::Reset(nullptr); return py_obj == nullptr; } -const char* +const char * PythonString::GetString() const { if (m_py_obj) @@ -154,7 +154,7 @@ } void -PythonString::SetString (const char* string) +PythonString::SetString(const char *string) { PythonObject::Reset(PyString_FromString(string)); } @@ -163,45 +163,44 @@ // PythonInteger //---------------------------------------------------------------------- -PythonInteger::PythonInteger (PyObject *py_obj) : - PythonObject() +PythonInteger::PythonInteger(PyObject *py_obj) + : PythonObject() { Reset(py_obj); // Use "Reset()" to ensure that py_obj is a integer type } -PythonInteger::PythonInteger (const PythonObject &object) : - PythonObject() +PythonInteger::PythonInteger(const PythonObject &object) + : PythonObject() { Reset(object.get()); // Use "Reset()" to ensure that py_obj is a integer type } -PythonInteger::PythonInteger (const lldb::ScriptInterpreterObjectSP &script_object_sp) : - PythonObject() +PythonInteger::PythonInteger(const lldb::ScriptInterpreterObjectSP &script_object_sp) + : PythonObject() { if (script_object_sp) Reset((PyObject *)script_object_sp->GetObject()); // Use "Reset()" to ensure that py_obj is a string } -PythonInteger::PythonInteger (int64_t value) : - PythonObject() +PythonInteger::PythonInteger(int64_t value) + : PythonObject() { - SetInteger (value); + SetInteger(value); } - -PythonInteger::~PythonInteger () +PythonInteger::~PythonInteger() { } bool -PythonInteger::Reset (PyObject *py_obj) +PythonInteger::Reset(PyObject *py_obj) { if (py_obj) { - if (PyInt_Check (py_obj) || PyLong_Check(py_obj)) + if (PyInt_Check(py_obj) || PyLong_Check(py_obj)) return PythonObject::Reset(py_obj); } - + PythonObject::Reset(nullptr); return py_obj == nullptr; } @@ -220,7 +219,7 @@ } void -PythonInteger::SetInteger (int64_t value) +PythonInteger::SetInteger(int64_t value) { PythonObject::Reset(PyLong_FromLongLong(value)); } @@ -229,46 +228,45 @@ // PythonList //---------------------------------------------------------------------- -PythonList::PythonList (bool create_empty) : - PythonObject(create_empty ? PyList_New(0) : nullptr) +PythonList::PythonList(bool create_empty) + : PythonObject(create_empty ? PyList_New(0) : nullptr) { } -PythonList::PythonList (uint32_t count) : - PythonObject(PyList_New(count)) +PythonList::PythonList(uint32_t count) + : PythonObject(PyList_New(count)) { } -PythonList::PythonList (PyObject *py_obj) : - PythonObject() +PythonList::PythonList(PyObject *py_obj) + : PythonObject() { Reset(py_obj); // Use "Reset()" to ensure that py_obj is a list } - -PythonList::PythonList (const PythonObject &object) : - PythonObject() +PythonList::PythonList(const PythonObject &object) + : PythonObject() { Reset(object.get()); // Use "Reset()" to ensure that py_obj is a list } -PythonList::PythonList (const lldb::ScriptInterpreterObjectSP &script_object_sp) : - PythonObject() +PythonList::PythonList(const lldb::ScriptInterpreterObjectSP &script_object_sp) + : PythonObject() { if (script_object_sp) Reset((PyObject *)script_object_sp->GetObject()); // Use "Reset()" to ensure that py_obj is a list } -PythonList::~PythonList () +PythonList::~PythonList() { } bool -PythonList::Reset (PyObject *py_obj) +PythonList::Reset(PyObject *py_obj) { if (py_obj && PyList_Check(py_obj)) return PythonObject::Reset(py_obj); - + PythonObject::Reset(nullptr); return py_obj == nullptr; } @@ -282,7 +280,7 @@ } PythonObject -PythonList::GetItemAtIndex (uint32_t index) +PythonList::GetItemAtIndex(uint32_t index) { if (m_py_obj) return PythonObject(PyList_GetItem(m_py_obj, index)); @@ -290,14 +288,14 @@ } void -PythonList::SetItemAtIndex (uint32_t index, const PythonObject & object) +PythonList::SetItemAtIndex(uint32_t index, const PythonObject &object) { if (m_py_obj && object) PyList_SetItem(m_py_obj, index, object.get()); } void -PythonList::AppendItem (const PythonObject &object) +PythonList::AppendItem(const PythonObject &object) { if (m_py_obj && object) PyList_Append(m_py_obj, object.get()); @@ -307,41 +305,40 @@ // PythonDictionary //---------------------------------------------------------------------- -PythonDictionary::PythonDictionary (bool create_empty) : -PythonObject(create_empty ? PyDict_New() : nullptr) +PythonDictionary::PythonDictionary(bool create_empty) + : PythonObject(create_empty ? PyDict_New() : nullptr) { } -PythonDictionary::PythonDictionary (PyObject *py_obj) : - PythonObject(py_obj) +PythonDictionary::PythonDictionary(PyObject *py_obj) + : PythonObject(py_obj) { Reset(py_obj); // Use "Reset()" to ensure that py_obj is a dictionary } - -PythonDictionary::PythonDictionary (const PythonObject &object) : - PythonObject() +PythonDictionary::PythonDictionary(const PythonObject &object) + : PythonObject() { Reset(object.get()); // Use "Reset()" to ensure that py_obj is a dictionary } -PythonDictionary::PythonDictionary (const lldb::ScriptInterpreterObjectSP &script_object_sp) : - PythonObject () +PythonDictionary::PythonDictionary(const lldb::ScriptInterpreterObjectSP &script_object_sp) + : PythonObject() { if (script_object_sp) Reset((PyObject *)script_object_sp->GetObject()); // Use "Reset()" to ensure that py_obj is a dictionary } -PythonDictionary::~PythonDictionary () +PythonDictionary::~PythonDictionary() { } bool -PythonDictionary::Reset (PyObject *py_obj) +PythonDictionary::Reset(PyObject *py_obj) { if (py_obj && PyDict_Check(py_obj)) return PythonObject::Reset(py_obj); - + PythonObject::Reset(nullptr); return py_obj == nullptr; } @@ -355,7 +352,7 @@ } PythonObject -PythonDictionary::GetItemForKey (const char *key) const +PythonDictionary::GetItemForKey(const char *key) const { if (key && key[0]) { @@ -365,18 +362,16 @@ return PythonObject(); } - PythonObject -PythonDictionary::GetItemForKey (const PythonString &key) const +PythonDictionary::GetItemForKey(const PythonString &key) const { if (m_py_obj && key) return PythonObject(PyDict_GetItem(m_py_obj, key.get())); return PythonObject(); } - const char * -PythonDictionary::GetItemForKeyAsString (const PythonString &key, const char *fail_value) const +PythonDictionary::GetItemForKeyAsString(const PythonString &key, const char *fail_value) const { if (m_py_obj && key) { @@ -388,7 +383,7 @@ } int64_t -PythonDictionary::GetItemForKeyAsInteger (const PythonString &key, int64_t fail_value) const +PythonDictionary::GetItemForKeyAsInteger(const PythonString &key, int64_t fail_value) const { if (m_py_obj && key) { @@ -406,7 +401,7 @@ } PythonList -PythonDictionary::GetKeys () const +PythonDictionary::GetKeys() const { if (m_py_obj) return PythonList(PyDict_Keys(m_py_obj)); @@ -414,11 +409,11 @@ } PythonString -PythonDictionary::GetKeyAtPosition (uint32_t pos) const +PythonDictionary::GetKeyAtPosition(uint32_t pos) const { PyObject *key, *value; Py_ssize_t pos_iter = 0; - + if (m_py_obj) { while (PyDict_Next(m_py_obj, &pos_iter, &key, &value)) @@ -431,15 +426,16 @@ } PythonObject -PythonDictionary::GetValueAtPosition (uint32_t pos) const +PythonDictionary::GetValueAtPosition(uint32_t pos) const { PyObject *key, *value; Py_ssize_t pos_iter = 0; - + if (!m_py_obj) return PythonObject(); - - while (PyDict_Next(m_py_obj, &pos_iter, &key, &value)) { + + while (PyDict_Next(m_py_obj, &pos_iter, &key, &value)) + { if (pos-- == 0) return PythonObject(value); } @@ -447,14 +443,14 @@ } void -PythonDictionary::SetItemForKey (const PythonString &key, PyObject *value) +PythonDictionary::SetItemForKey(const PythonString &key, PyObject *value) { if (m_py_obj && key && value) PyDict_SetItem(m_py_obj, key.get(), value); } void -PythonDictionary::SetItemForKey (const PythonString &key, const PythonObject &value) +PythonDictionary::SetItemForKey(const PythonString &key, const PythonObject &value) { if (m_py_obj && key && value) PyDict_SetItem(m_py_obj, key.get(), value.get()); Index: source/ScriptInterpreter/Python/ScriptInterpreterPython.cpp =================================================================== --- source/ScriptInterpreter/Python/ScriptInterpreterPython.cpp +++ source/ScriptInterpreter/Python/ScriptInterpreterPython.cpp @@ -16,7 +16,7 @@ #else #include "lldb/lldb-python.h" -#include "lldb/Interpreter/ScriptInterpreterPython.h" +#include "lldb/ScriptInterpreter/Python/ScriptInterpreterPython.h" #include #include @@ -35,7 +35,7 @@ #include "lldb/Host/Pipe.h" #include "lldb/Interpreter/CommandInterpreter.h" #include "lldb/Interpreter/CommandReturnObject.h" -#include "lldb/Interpreter/PythonDataObjects.h" +#include "lldb/ScriptInterpreter/Python/PythonDataObjects.h" #include "lldb/Target/Thread.h" #include "lldb/Target/ThreadPlan.h" @@ -46,44 +46,82 @@ using namespace lldb; using namespace lldb_private; +// these are the Pythonic implementations of the required callbacks +// these are scripting-language specific, which is why they belong here +// we still need to use function pointers to them instead of relying +// on linkage-time resolution because the SWIG stuff and this file +// get built at different times +// Defined in the SWIG source file +extern "C" void init_lldb(void); -static ScriptInterpreter::SWIGInitCallback g_swig_init_callback = nullptr; -static ScriptInterpreter::SWIGBreakpointCallbackFunction g_swig_breakpoint_callback = nullptr; -static ScriptInterpreter::SWIGWatchpointCallbackFunction g_swig_watchpoint_callback = nullptr; -static ScriptInterpreter::SWIGPythonTypeScriptCallbackFunction g_swig_typescript_callback = nullptr; -static ScriptInterpreter::SWIGPythonCreateSyntheticProvider g_swig_synthetic_script = nullptr; -static ScriptInterpreter::SWIGPythonCalculateNumChildren g_swig_calc_children = nullptr; -static ScriptInterpreter::SWIGPythonGetChildAtIndex g_swig_get_child_index = nullptr; -static ScriptInterpreter::SWIGPythonGetIndexOfChildWithName g_swig_get_index_child = nullptr; -static ScriptInterpreter::SWIGPythonCastPyObjectToSBValue g_swig_cast_to_sbvalue = nullptr; -static ScriptInterpreter::SWIGPythonGetValueObjectSPFromSBValue g_swig_get_valobj_sp_from_sbvalue = nullptr; -static ScriptInterpreter::SWIGPythonUpdateSynthProviderInstance g_swig_update_provider = nullptr; -static ScriptInterpreter::SWIGPythonMightHaveChildrenSynthProviderInstance g_swig_mighthavechildren_provider = nullptr; -static ScriptInterpreter::SWIGPythonGetValueSynthProviderInstance g_swig_getvalue_provider = nullptr; -static ScriptInterpreter::SWIGPythonCallCommand g_swig_call_command = nullptr; -static ScriptInterpreter::SWIGPythonCallModuleInit g_swig_call_module_init = nullptr; -static ScriptInterpreter::SWIGPythonCreateOSPlugin g_swig_create_os_plugin = nullptr; -static ScriptInterpreter::SWIGPythonScriptKeyword_Process g_swig_run_script_keyword_process = nullptr; -static ScriptInterpreter::SWIGPythonScriptKeyword_Thread g_swig_run_script_keyword_thread = nullptr; -static ScriptInterpreter::SWIGPythonScriptKeyword_Target g_swig_run_script_keyword_target = nullptr; -static ScriptInterpreter::SWIGPythonScriptKeyword_Frame g_swig_run_script_keyword_frame = nullptr; -static ScriptInterpreter::SWIGPythonScriptKeyword_Value g_swig_run_script_keyword_value = nullptr; -static ScriptInterpreter::SWIGPython_GetDynamicSetting g_swig_plugin_get = nullptr; -static ScriptInterpreter::SWIGPythonCreateScriptedThreadPlan g_swig_thread_plan_script = nullptr; -static ScriptInterpreter::SWIGPythonCallThreadPlan g_swig_call_thread_plan = nullptr; +extern "C" bool LLDBSwigPythonBreakpointCallbackFunction(const char *python_function_name, const char *session_dictionary_name, + const lldb::StackFrameSP &sb_frame, const lldb::BreakpointLocationSP &sb_bp_loc); -static std::string -ReadPythonBacktrace (PyObject* py_backtrace); - -ScriptInterpreterPython::Locker::Locker (ScriptInterpreterPython *py_interpreter, - uint16_t on_entry, - uint16_t on_leave, - FILE *in, - FILE *out, - FILE *err) : - ScriptInterpreterLocker (), - m_teardown_session( (on_leave & TearDownSession) == TearDownSession ), - m_python_interpreter(py_interpreter) +extern "C" bool LLDBSwigPythonWatchpointCallbackFunction(const char *python_function_name, const char *session_dictionary_name, + const lldb::StackFrameSP &sb_frame, const lldb::WatchpointSP &sb_wp); + +extern "C" bool LLDBSwigPythonCallTypeScript(const char *python_function_name, void *session_dictionary, + const lldb::ValueObjectSP &valobj_sp, void **pyfunct_wrapper, + const lldb::TypeSummaryOptionsSP &options_sp, std::string &retval); + +extern "C" void *LLDBSwigPythonCreateSyntheticProvider(const char *python_class_name, const char *session_dictionary_name, + const lldb::ValueObjectSP &valobj_sp); + +extern "C" void *LLDBSwigPythonCreateScriptedThreadPlan(const char *python_class_name, const char *session_dictionary_name, + const lldb::ThreadPlanSP &thread_plan_sp); + +extern "C" bool LLDBSWIGPythonCallThreadPlan(void *implementor, const char *method_name, Event *event_sp, bool &got_error); + +extern "C" uint32_t LLDBSwigPython_CalculateNumChildren(void *implementor); + +extern "C" void *LLDBSwigPython_GetChildAtIndex(void *implementor, uint32_t idx); + +extern "C" int LLDBSwigPython_GetIndexOfChildWithName(void *implementor, const char *child_name); + +extern "C" void *LLDBSWIGPython_CastPyObjectToSBValue(void *data); + +extern lldb::ValueObjectSP LLDBSWIGPython_GetValueObjectSPFromSBValue(void *data); + +extern "C" bool LLDBSwigPython_UpdateSynthProviderInstance(void *implementor); + +extern "C" bool LLDBSwigPython_MightHaveChildrenSynthProviderInstance(void *implementor); + +extern "C" void *LLDBSwigPython_GetValueSynthProviderInstance(void *implementor); + +extern "C" bool LLDBSwigPythonCallCommand(const char *python_function_name, const char *session_dictionary_name, lldb::DebuggerSP &debugger, + const char *args, lldb_private::CommandReturnObject &cmd_retobj, + lldb::ExecutionContextRefSP exe_ctx_ref_sp); + +extern "C" bool LLDBSwigPythonCallModuleInit(const char *python_module_name, const char *session_dictionary_name, + lldb::DebuggerSP &debugger); + +extern "C" void *LLDBSWIGPythonCreateOSPlugin(const char *python_class_name, const char *session_dictionary_name, + const lldb::ProcessSP &process_sp); + +extern "C" bool LLDBSWIGPythonRunScriptKeywordProcess(const char *python_function_name, const char *session_dictionary_name, + lldb::ProcessSP &process, std::string &output); + +extern "C" bool LLDBSWIGPythonRunScriptKeywordThread(const char *python_function_name, const char *session_dictionary_name, + lldb::ThreadSP &thread, std::string &output); + +extern "C" bool LLDBSWIGPythonRunScriptKeywordTarget(const char *python_function_name, const char *session_dictionary_name, + lldb::TargetSP &target, std::string &output); + +extern "C" bool LLDBSWIGPythonRunScriptKeywordFrame(const char *python_function_name, const char *session_dictionary_name, + lldb::StackFrameSP &frame, std::string &output); + +extern "C" bool LLDBSWIGPythonRunScriptKeywordValue(const char *python_function_name, const char *session_dictionary_name, + lldb::ValueObjectSP &value, std::string &output); + +extern "C" void *LLDBSWIGPython_GetDynamicSetting(void *module, const char *setting, const lldb::TargetSP &target_sp); + +static std::string ReadPythonBacktrace(PyObject *py_backtrace); + +ScriptInterpreterPython::Locker::Locker(ScriptInterpreterPython *py_interpreter, uint16_t on_entry, uint16_t on_leave, FILE *in, FILE *out, + FILE *err) + : ScriptInterpreterLocker() + , m_teardown_session((on_leave & TearDownSession) == TearDownSession) + , m_python_interpreter(py_interpreter) { DoAcquireLock(); if ((on_entry & InitSession) == InitSession) @@ -149,27 +187,28 @@ DoFreeLock(); } - -ScriptInterpreterPython::ScriptInterpreterPython (CommandInterpreter &interpreter) : - ScriptInterpreter (interpreter, eScriptLanguagePython), - IOHandlerDelegateMultiline("DONE"), - m_saved_stdin (), - m_saved_stdout (), - m_saved_stderr (), - m_main_module (), - m_lldb_module (), - m_session_dict (false), // Don't create an empty dictionary, leave it invalid - m_sys_module_dict (false), // Don't create an empty dictionary, leave it invalid - m_run_one_line_function (), - m_run_one_line_str_global (), - m_dictionary_name (interpreter.GetDebugger().GetInstanceName().AsCString()), - m_terminal_state (), - m_active_io_handler (eIOHandlerNone), - m_session_is_active (false), - m_pty_slave_is_open (false), - m_valid_session (true), - m_lock_count (0), - m_command_thread_state (nullptr) +ScriptInterpreterPython::ScriptInterpreterPython(CommandInterpreter &interpreter) + : ScriptInterpreter(interpreter, eScriptLanguagePython) + , IOHandlerDelegateMultiline("DONE") + , m_saved_stdin() + , m_saved_stdout() + , m_saved_stderr() + , m_main_module() + , m_lldb_module() + , m_session_dict(false) + , // Don't create an empty dictionary, leave it invalid + m_sys_module_dict(false) + , // Don't create an empty dictionary, leave it invalid + m_run_one_line_function() + , m_run_one_line_str_global() + , m_dictionary_name(interpreter.GetDebugger().GetInstanceName().AsCString()) + , m_terminal_state() + , m_active_io_handler(eIOHandlerNone) + , m_session_is_active(false) + , m_pty_slave_is_open(false) + , m_valid_session(true) + , m_lock_count(0) + , m_command_thread_state(nullptr) { ScriptInterpreterPython::InitializePrivate (); @@ -178,9 +217,7 @@ StreamString run_string; run_string.Printf ("%s = dict()", m_dictionary_name.c_str()); - Locker locker(this, - ScriptInterpreterPython::Locker::AcquireLock, - ScriptInterpreterPython::Locker::FreeAcquiredLock); + Locker locker(this, ScriptInterpreterPython::Locker::AcquireLock, ScriptInterpreterPython::Locker::FreeAcquiredLock); PyRun_SimpleString (run_string.GetData()); run_string.Clear(); @@ -210,12 +247,14 @@ if (new_count > old_count) Debugger::Terminate(); - run_string.Printf ("run_one_line (%s, 'import lldb.embedded_interpreter; from lldb.embedded_interpreter import run_python_interpreter; from lldb.embedded_interpreter import run_one_line')", m_dictionary_name.c_str()); + run_string.Printf("run_one_line (%s, 'import lldb.embedded_interpreter; from lldb.embedded_interpreter import run_python_interpreter; " + "from lldb.embedded_interpreter import run_one_line')", + m_dictionary_name.c_str()); PyRun_SimpleString (run_string.GetData()); run_string.Clear(); - run_string.Printf ("run_one_line (%s, 'lldb.debugger_unique_id = %" PRIu64 "; pydoc.pager = pydoc.plainpager')", m_dictionary_name.c_str(), - interpreter.GetDebugger().GetID()); + run_string.Printf("run_one_line (%s, 'lldb.debugger_unique_id = %" PRIu64 "; pydoc.pager = pydoc.plainpager')", + m_dictionary_name.c_str(), interpreter.GetDebugger().GetID()); PyRun_SimpleString (run_string.GetData()); } @@ -327,7 +366,6 @@ } } - void ScriptInterpreterPython::ResetOutputFileHandle (FILE *fh) { @@ -394,10 +432,7 @@ } bool -ScriptInterpreterPython::EnterSession (uint16_t on_entry_flags, - FILE *in, - FILE *out, - FILE *err) +ScriptInterpreterPython::EnterSession(uint16_t on_entry_flags, FILE *in, FILE *out, FILE *err) { // If we have already entered the session, without having officially 'left' it, then there is no need to // 'enter' it again. @@ -405,22 +440,25 @@ if (m_session_is_active) { if (log) - log->Printf("ScriptInterpreterPython::EnterSession(on_entry_flags=0x%" PRIx16 ") session is already active, returning without doing anything", on_entry_flags); + log->Printf("ScriptInterpreterPython::EnterSession(on_entry_flags=0x%" PRIx16 + ") session is already active, returning without doing anything", + on_entry_flags); return false; } if (log) log->Printf("ScriptInterpreterPython::EnterSession(on_entry_flags=0x%" PRIx16 ")", on_entry_flags); - m_session_is_active = true; StreamString run_string; if (on_entry_flags & Locker::InitGlobals) { - run_string.Printf ( "run_one_line (%s, 'lldb.debugger_unique_id = %" PRIu64, m_dictionary_name.c_str(), GetCommandInterpreter().GetDebugger().GetID()); - run_string.Printf ( "; lldb.debugger = lldb.SBDebugger.FindDebuggerWithID (%" PRIu64 ")", GetCommandInterpreter().GetDebugger().GetID()); + run_string.Printf("run_one_line (%s, 'lldb.debugger_unique_id = %" PRIu64, m_dictionary_name.c_str(), + GetCommandInterpreter().GetDebugger().GetID()); + run_string.Printf("; lldb.debugger = lldb.SBDebugger.FindDebuggerWithID (%" PRIu64 ")", + GetCommandInterpreter().GetDebugger().GetID()); run_string.PutCString ("; lldb.target = lldb.debugger.GetSelectedTarget()"); run_string.PutCString ("; lldb.process = lldb.target.GetProcess()"); run_string.PutCString ("; lldb.thread = lldb.process.GetSelectedThread ()"); @@ -430,8 +468,10 @@ else { // If we aren't initing the globals, we should still always set the debugger (since that is always unique.) - run_string.Printf ( "run_one_line (%s, 'lldb.debugger_unique_id = %" PRIu64, m_dictionary_name.c_str(), GetCommandInterpreter().GetDebugger().GetID()); - run_string.Printf ( "; lldb.debugger = lldb.SBDebugger.FindDebuggerWithID (%" PRIu64 ")", GetCommandInterpreter().GetDebugger().GetID()); + run_string.Printf("run_one_line (%s, 'lldb.debugger_unique_id = %" PRIu64, m_dictionary_name.c_str(), + GetCommandInterpreter().GetDebugger().GetID()); + run_string.Printf("; lldb.debugger = lldb.SBDebugger.FindDebuggerWithID (%" PRIu64 ")", + GetCommandInterpreter().GetDebugger().GetID()); run_string.PutCString ("')"); } @@ -536,9 +576,7 @@ } static std::string -GenerateUniqueName (const char* base_name_wanted, - uint32_t& functions_counter, - const void* name_token = nullptr) +GenerateUniqueName(const char *base_name_wanted, uint32_t &functions_counter, const void *name_token = nullptr) { StreamString sstr; @@ -652,16 +690,11 @@ FILE *in_file = input_file_sp->GetFile().GetStream(); FILE *out_file = output_file_sp->GetFile().GetStream(); FILE *err_file = error_file_sp->GetFile().GetStream(); - Locker locker(this, - ScriptInterpreterPython::Locker::AcquireLock | - ScriptInterpreterPython::Locker::InitSession | + Locker locker(this, ScriptInterpreterPython::Locker::AcquireLock | ScriptInterpreterPython::Locker::InitSession | (options.GetSetLLDBGlobals() ? ScriptInterpreterPython::Locker::InitGlobals : 0) | ((result && result->GetInteractive()) ? 0: Locker::NoSTDIN), - ScriptInterpreterPython::Locker::FreeAcquiredLock | - ScriptInterpreterPython::Locker::TearDownSession, - in_file, - out_file, - err_file); + ScriptInterpreterPython::Locker::FreeAcquiredLock | ScriptInterpreterPython::Locker::TearDownSession, in_file, + out_file, err_file); bool success = false; @@ -709,7 +742,6 @@ output_comm.Disconnect(); } - if (success) return true; @@ -724,25 +756,16 @@ return false; } - -class IOHandlerPythonInterpreter : - public IOHandler +class IOHandlerPythonInterpreter : public IOHandler { public: - - IOHandlerPythonInterpreter (Debugger &debugger, - ScriptInterpreterPython *python) : - IOHandler (debugger, IOHandler::Type::PythonInterpreter), - m_python(python) + IOHandlerPythonInterpreter(Debugger &debugger, ScriptInterpreterPython *python) + : IOHandler(debugger, IOHandler::Type::PythonInterpreter) + , m_python(python) { - } - virtual - ~IOHandlerPythonInterpreter() - { - - } + virtual ~IOHandlerPythonInterpreter() {} virtual ConstString GetControlSequence (char ch) @@ -771,12 +794,10 @@ terminal.SetEcho(true); } - ScriptInterpreterPython::Locker locker (m_python, - ScriptInterpreterPython::Locker::AcquireLock | - ScriptInterpreterPython::Locker::InitSession | + ScriptInterpreterPython::Locker locker( + m_python, ScriptInterpreterPython::Locker::AcquireLock | ScriptInterpreterPython::Locker::InitSession | ScriptInterpreterPython::Locker::InitGlobals, - ScriptInterpreterPython::Locker::FreeAcquiredLock | - ScriptInterpreterPython::Locker::TearDownSession); + ScriptInterpreterPython::Locker::FreeAcquiredLock | ScriptInterpreterPython::Locker::TearDownSession); // The following call drops into the embedded interpreter loop and stays there until the // user chooses to exit from the Python interpreter. @@ -802,19 +823,16 @@ virtual void Hide () { - } virtual void Refresh () { - } virtual void Cancel () { - } virtual bool @@ -826,13 +844,12 @@ virtual void GotEOF() { - } + protected: ScriptInterpreterPython *m_python; }; - void ScriptInterpreterPython::ExecuteInterpreterLoop () { @@ -871,24 +888,22 @@ _PyThreadState_Current = state; int num_threads = PyThreadState_SetAsyncExc(tid, PyExc_KeyboardInterrupt); if (log) - log->Printf("ScriptInterpreterPython::Interrupt() sending PyExc_KeyboardInterrupt (tid = %li, num_threads = %i)...", tid, num_threads); + log->Printf("ScriptInterpreterPython::Interrupt() sending PyExc_KeyboardInterrupt (tid = %li, num_threads = %i)...", tid, + num_threads); return true; } } if (log) log->Printf("ScriptInterpreterPython::Interrupt() python code not running, can't interrupt"); return false; - } bool -ScriptInterpreterPython::ExecuteOneLineWithReturn (const char *in_string, - ScriptInterpreter::ScriptReturnType return_type, - void *ret_value, +ScriptInterpreterPython::ExecuteOneLineWithReturn(const char *in_string, ScriptInterpreter::ScriptReturnType return_type, void *ret_value, const ExecuteScriptOptions &options) { - Locker locker(this, - ScriptInterpreterPython::Locker::AcquireLock | ScriptInterpreterPython::Locker::InitSession | (options.GetSetLLDBGlobals() ? ScriptInterpreterPython::Locker::InitGlobals : 0) | Locker::NoSTDIN, + Locker locker(this, ScriptInterpreterPython::Locker::AcquireLock | ScriptInterpreterPython::Locker::InitSession | + (options.GetSetLLDBGlobals() ? ScriptInterpreterPython::Locker::InitGlobals : 0) | Locker::NoSTDIN, ScriptInterpreterPython::Locker::FreeAcquiredLock | ScriptInterpreterPython::Locker::TearDownSession); PyObject *py_return = nullptr; @@ -1051,8 +1066,8 @@ { Error error; - Locker locker(this, - ScriptInterpreterPython::Locker::AcquireLock | ScriptInterpreterPython::Locker::InitSession | (options.GetSetLLDBGlobals() ? ScriptInterpreterPython::Locker::InitGlobals : 0) | Locker::NoSTDIN, + Locker locker(this, ScriptInterpreterPython::Locker::AcquireLock | ScriptInterpreterPython::Locker::InitSession | + (options.GetSetLLDBGlobals() ? ScriptInterpreterPython::Locker::InitGlobals : 0) | Locker::NoSTDIN, ScriptInterpreterPython::Locker::FreeAcquiredLock | ScriptInterpreterPython::Locker::TearDownSession); PythonObject return_value; @@ -1121,7 +1136,6 @@ return error; } - void ScriptInterpreterPython::CollectDataForBreakpointCommandCallback (std::vector &bp_options_vec, CommandReturnObject &result) @@ -1131,29 +1145,25 @@ } void -ScriptInterpreterPython::CollectDataForWatchpointCommandCallback (WatchpointOptions *wp_options, - CommandReturnObject &result) +ScriptInterpreterPython::CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options, CommandReturnObject &result) { m_active_io_handler = eIOHandlerWatchpoint; m_interpreter.GetPythonCommandsFromIOHandler (" ", *this, true, wp_options); } void -ScriptInterpreterPython::SetBreakpointCommandCallbackFunction (BreakpointOptions *bp_options, - const char *function_name) +ScriptInterpreterPython::SetBreakpointCommandCallbackFunction(BreakpointOptions *bp_options, const char *function_name) { // For now just cons up a oneliner that calls the provided function. std::string oneliner("return "); oneliner += function_name; oneliner += "(frame, bp_loc, internal_dict)"; - m_interpreter.GetScriptInterpreter()->SetBreakpointCommandCallback (bp_options, - oneliner.c_str()); + m_interpreter.GetScriptInterpreter()->SetBreakpointCommandCallback(bp_options, oneliner.c_str()); } // Set a Python one-liner as the callback for the breakpoint. Error -ScriptInterpreterPython::SetBreakpointCommandCallback (BreakpointOptions *bp_options, - const char *command_body_text) +ScriptInterpreterPython::SetBreakpointCommandCallback(BreakpointOptions *bp_options, const char *command_body_text) { std::unique_ptr data_ap(new BreakpointOptions::CommandData()); @@ -1175,8 +1185,7 @@ // Set a Python one-liner as the callback for the watchpoint. void -ScriptInterpreterPython::SetWatchpointCommandCallback (WatchpointOptions *wp_options, - const char *oneliner) +ScriptInterpreterPython::SetWatchpointCommandCallback(WatchpointOptions *wp_options, const char *oneliner) { std::unique_ptr data_ap(new WatchpointOptions::CommandData()); @@ -1301,7 +1310,6 @@ return true; } - bool ScriptInterpreterPython::GenerateTypeSynthClass (StringList &user_input, std::string &output, const void* name_token) { @@ -1335,7 +1343,6 @@ auto_generated_class.AppendString (sstr.GetData()); } - // Verify that the results are valid Python. // (even though the method is ExportFunctionDefinitionToInterpreter, a class will actually be exported) // (TODO: rename that method to ExportDefinitionToInterpreter) @@ -1360,12 +1367,8 @@ void* ret_val; { - Locker py_lock (this, - Locker::AcquireLock | Locker::NoSTDIN, - Locker::FreeLock); - ret_val = g_swig_create_os_plugin (class_name, - m_dictionary_name.c_str(), - process_sp); + Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock); + ret_val = LLDBSWIGPythonCreateOSPlugin(class_name, m_dictionary_name.c_str(), process_sp); } return MakeScriptObject(ret_val); @@ -1374,9 +1377,7 @@ lldb::ScriptInterpreterObjectSP ScriptInterpreterPython::OSPlugin_RegisterInfo (lldb::ScriptInterpreterObjectSP os_plugin_object_sp) { - Locker py_lock(this, - Locker::AcquireLock | Locker::NoSTDIN, - Locker::FreeLock); + Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock); static char callee_name[] = "get_register_info"; @@ -1435,9 +1436,7 @@ lldb::ScriptInterpreterObjectSP ScriptInterpreterPython::OSPlugin_ThreadsInfo (lldb::ScriptInterpreterObjectSP os_plugin_object_sp) { - Locker py_lock (this, - Locker::AcquireLock | Locker::NoSTDIN, - Locker::FreeLock); + Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock); static char callee_name[] = "get_thread_info"; @@ -1500,32 +1499,96 @@ // change. template -const char *GetPythonValueFormatString(T t) +const char * +GetPythonValueFormatString(T t) { - assert(!"Unhandled type passed to GetPythonValueFormatString(T), make a specialization of GetPythonValueFormatString() to support this type."); + assert(!"Unhandled type passed to GetPythonValueFormatString(T), make a specialization of GetPythonValueFormatString() to support this " + "type."); return nullptr; } -template <> const char *GetPythonValueFormatString (char *) { return "s"; } -template <> const char *GetPythonValueFormatString (char) { return "b"; } -template <> const char *GetPythonValueFormatString (unsigned char) { return "B"; } -template <> const char *GetPythonValueFormatString (short) { return "h"; } -template <> const char *GetPythonValueFormatString (unsigned short) { return "H"; } -template <> const char *GetPythonValueFormatString (int) { return "i"; } -template <> const char *GetPythonValueFormatString (unsigned int) { return "I"; } -template <> const char *GetPythonValueFormatString (long) { return "l"; } -template <> const char *GetPythonValueFormatString (unsigned long) { return "k"; } -template <> const char *GetPythonValueFormatString (long long) { return "L"; } -template <> const char *GetPythonValueFormatString (unsigned long long) { return "K"; } -template <> const char *GetPythonValueFormatString (float t) { return "f"; } -template <> const char *GetPythonValueFormatString (double t) { return "d"; } +template <> +const char * +GetPythonValueFormatString(char *) +{ + return "s"; +} +template <> +const char * +GetPythonValueFormatString(char) +{ + return "b"; +} +template <> +const char * +GetPythonValueFormatString(unsigned char) +{ + return "B"; +} +template <> +const char * +GetPythonValueFormatString(short) +{ + return "h"; +} +template <> +const char * +GetPythonValueFormatString(unsigned short) +{ + return "H"; +} +template <> +const char * +GetPythonValueFormatString(int) +{ + return "i"; +} +template <> +const char * +GetPythonValueFormatString(unsigned int) +{ + return "I"; +} +template <> +const char * +GetPythonValueFormatString(long) +{ + return "l"; +} +template <> +const char * +GetPythonValueFormatString(unsigned long) +{ + return "k"; +} +template <> +const char * +GetPythonValueFormatString(long long) +{ + return "L"; +} +template <> +const char * +GetPythonValueFormatString(unsigned long long) +{ + return "K"; +} +template <> +const char * +GetPythonValueFormatString(float t) +{ + return "f"; +} +template <> +const char * +GetPythonValueFormatString(double t) +{ + return "d"; +} lldb::ScriptInterpreterObjectSP -ScriptInterpreterPython::OSPlugin_RegisterContextData (lldb::ScriptInterpreterObjectSP os_plugin_object_sp, - lldb::tid_t tid) +ScriptInterpreterPython::OSPlugin_RegisterContextData(lldb::ScriptInterpreterObjectSP os_plugin_object_sp, lldb::tid_t tid) { - Locker py_lock (this, - Locker::AcquireLock | Locker::NoSTDIN, - Locker::FreeLock); + Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock); static char callee_name[] = "get_register_data"; static char *param_format = const_cast(GetPythonValueFormatString(tid)); @@ -1583,13 +1646,9 @@ } lldb::ScriptInterpreterObjectSP -ScriptInterpreterPython::OSPlugin_CreateThread (lldb::ScriptInterpreterObjectSP os_plugin_object_sp, - lldb::tid_t tid, - lldb::addr_t context) +ScriptInterpreterPython::OSPlugin_CreateThread(lldb::ScriptInterpreterObjectSP os_plugin_object_sp, lldb::tid_t tid, lldb::addr_t context) { - Locker py_lock(this, - Locker::AcquireLock | Locker::NoSTDIN, - Locker::FreeLock); + Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock); static char callee_name[] = "create_thread"; std::string param_format; @@ -1649,8 +1708,7 @@ } lldb::ScriptInterpreterObjectSP -ScriptInterpreterPython::CreateScriptedThreadPlan (const char *class_name, - lldb::ThreadPlanSP thread_plan_sp) +ScriptInterpreterPython::CreateScriptedThreadPlan(const char *class_name, lldb::ThreadPlanSP thread_plan_sp) { if (class_name == nullptr || class_name[0] == '\0') return lldb::ScriptInterpreterObjectSP(); @@ -1670,24 +1728,20 @@ { Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); - ret_val = g_swig_thread_plan_script (class_name, - python_interpreter->m_dictionary_name.c_str(), - thread_plan_sp); + ret_val = LLDBSwigPythonCreateScriptedThreadPlan(class_name, python_interpreter->m_dictionary_name.c_str(), thread_plan_sp); } return MakeScriptObject(ret_val); } bool -ScriptInterpreterPython::ScriptedThreadPlanExplainsStop (lldb::ScriptInterpreterObjectSP implementor_sp, - Event *event, - bool &script_error) +ScriptInterpreterPython::ScriptedThreadPlanExplainsStop(lldb::ScriptInterpreterObjectSP implementor_sp, Event *event, bool &script_error) { bool explains_stop = true; if (implementor_sp) { Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); - explains_stop = g_swig_call_thread_plan (implementor_sp->GetObject(), "explains_stop", event, script_error); + explains_stop = LLDBSWIGPythonCallThreadPlan(implementor_sp->GetObject(), "explains_stop", event, script_error); if (script_error) return true; } @@ -1695,15 +1749,13 @@ } bool -ScriptInterpreterPython::ScriptedThreadPlanShouldStop (lldb::ScriptInterpreterObjectSP implementor_sp, - Event *event, - bool &script_error) +ScriptInterpreterPython::ScriptedThreadPlanShouldStop(lldb::ScriptInterpreterObjectSP implementor_sp, Event *event, bool &script_error) { bool should_stop = true; if (implementor_sp) { Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); - should_stop = g_swig_call_thread_plan (implementor_sp->GetObject(), "should_stop", event, script_error); + should_stop = LLDBSWIGPythonCallThreadPlan(implementor_sp->GetObject(), "should_stop", event, script_error); if (script_error) return true; } @@ -1711,14 +1763,13 @@ } lldb::StateType -ScriptInterpreterPython::ScriptedThreadPlanGetRunState (lldb::ScriptInterpreterObjectSP implementor_sp, - bool &script_error) +ScriptInterpreterPython::ScriptedThreadPlanGetRunState(lldb::ScriptInterpreterObjectSP implementor_sp, bool &script_error) { bool should_step = false; if (implementor_sp) { Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); - should_step = g_swig_call_thread_plan (implementor_sp->GetObject(), "should_step", NULL, script_error); + should_step = LLDBSWIGPythonCallThreadPlan(implementor_sp->GetObject(), "should_step", NULL, script_error); if (script_error) should_step = true; } @@ -1728,10 +1779,8 @@ return lldb::eStateRunning; } - lldb::ScriptInterpreterObjectSP -ScriptInterpreterPython::LoadPluginModule (const FileSpec& file_spec, - lldb_private::Error& error) +ScriptInterpreterPython::LoadPluginModule(const FileSpec &file_spec, lldb_private::Error &error) { if (!file_spec.Exists()) { @@ -1748,31 +1797,25 @@ } lldb::ScriptInterpreterObjectSP -ScriptInterpreterPython::GetDynamicSettings (lldb::ScriptInterpreterObjectSP plugin_module_sp, - Target* target, - const char* setting_name, +ScriptInterpreterPython::GetDynamicSettings(lldb::ScriptInterpreterObjectSP plugin_module_sp, Target *target, const char *setting_name, lldb_private::Error& error) { if (!plugin_module_sp || !target || !setting_name || !setting_name[0]) return lldb::ScriptInterpreterObjectSP(); - if (!g_swig_plugin_get) - return lldb::ScriptInterpreterObjectSP(); - PyObject *reply_pyobj = nullptr; { Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); TargetSP target_sp(target->shared_from_this()); - reply_pyobj = (PyObject*)g_swig_plugin_get(plugin_module_sp->GetObject(),setting_name,target_sp); + reply_pyobj = (PyObject *)LLDBSWIGPython_GetDynamicSetting(plugin_module_sp->GetObject(), setting_name, target_sp); } return MakeScriptObject(reply_pyobj); } lldb::ScriptInterpreterObjectSP -ScriptInterpreterPython::CreateSyntheticScriptedProvider (const char *class_name, - lldb::ValueObjectSP valobj) +ScriptInterpreterPython::CreateSyntheticScriptedProvider(const char *class_name, lldb::ValueObjectSP valobj) { if (class_name == nullptr || class_name[0] == '\0') return lldb::ScriptInterpreterObjectSP(); @@ -1797,9 +1840,7 @@ { Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); - ret_val = g_swig_synthetic_script (class_name, - python_interpreter->m_dictionary_name.c_str(), - valobj); + ret_val = LLDBSwigPythonCreateSyntheticProvider(class_name, python_interpreter->m_dictionary_name.c_str(), valobj); } return MakeScriptObject(ret_val); @@ -1821,7 +1862,6 @@ return GenerateTypeSynthClass(input, output, name_token); } - Error ScriptInterpreterPython::GenerateBreakpointCommandCallbackData (StringList &user_input, std::string& output) { @@ -1869,10 +1909,8 @@ } bool -ScriptInterpreterPython::GetScriptedSummary (const char *python_function_name, - lldb::ValueObjectSP valobj, - lldb::ScriptInterpreterObjectSP& callee_wrapper_sp, - const TypeSummaryOptions& options, +ScriptInterpreterPython::GetScriptedSummary(const char *python_function_name, lldb::ValueObjectSP valobj, + lldb::ScriptInterpreterObjectSP &callee_wrapper_sp, const TypeSummaryOptions &options, std::string& retval) { @@ -1888,20 +1926,15 @@ void* new_callee = old_callee; bool ret_val; - if (python_function_name - && *python_function_name) + if (python_function_name && *python_function_name) { { Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); { TypeSummaryOptionsSP options_sp(new TypeSummaryOptions(options)); - Timer scoped_timer ("g_swig_typescript_callback","g_swig_typescript_callback"); - ret_val = g_swig_typescript_callback (python_function_name, - GetSessionDictionary().get(), - valobj, - &new_callee, - options_sp, + Timer scoped_timer("LLDBSwigPythonCallTypeScript", "LLDBSwigPythonCallTypeScript"); + ret_val = LLDBSwigPythonCallTypeScript(python_function_name, GetSessionDictionary().get(), valobj, &new_callee, options_sp, retval); } } @@ -1916,7 +1949,6 @@ callee_wrapper_sp = MakeScriptObject(new_callee); return ret_val; - } void @@ -1924,9 +1956,7 @@ { // Release any global variables that might have strong references to // LLDB objects when clearing the python script interpreter. - Locker locker(this, - ScriptInterpreterPython::Locker::AcquireLock, - ScriptInterpreterPython::Locker::FreeAcquiredLock); + Locker locker(this, ScriptInterpreterPython::Locker::AcquireLock, ScriptInterpreterPython::Locker::FreeAcquiredLock); // This may be called as part of Py_Finalize. In that case the modules are destroyed in random // order and we can't guarantee that we can access these. @@ -1935,13 +1965,8 @@ } bool -ScriptInterpreterPython::BreakpointCallbackFunction -( - void *baton, - StoppointCallbackContext *context, - user_id_t break_id, - user_id_t break_loc_id -) +ScriptInterpreterPython::BreakpointCallbackFunction(void *baton, StoppointCallbackContext *context, user_id_t break_id, + user_id_t break_loc_id) { BreakpointOptions::CommandData *bp_option_data = (BreakpointOptions::CommandData *) baton; const char *python_function_name = bp_option_data->script_source.c_str(); @@ -1975,10 +2000,8 @@ bool ret_val = true; { Locker py_lock(python_interpreter, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); - ret_val = g_swig_breakpoint_callback (python_function_name, - python_interpreter->m_dictionary_name.c_str(), - stop_frame_sp, - bp_loc_sp); + ret_val = LLDBSwigPythonBreakpointCallbackFunction(python_function_name, python_interpreter->m_dictionary_name.c_str(), + stop_frame_sp, bp_loc_sp); } return ret_val; } @@ -1990,12 +2013,7 @@ } bool -ScriptInterpreterPython::WatchpointCallbackFunction -( - void *baton, - StoppointCallbackContext *context, - user_id_t watch_id -) +ScriptInterpreterPython::WatchpointCallbackFunction(void *baton, StoppointCallbackContext *context, user_id_t watch_id) { WatchpointOptions::CommandData *wp_option_data = (WatchpointOptions::CommandData *) baton; const char *python_function_name = wp_option_data->script_source.c_str(); @@ -2027,10 +2045,8 @@ bool ret_val = true; { Locker py_lock(python_interpreter, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); - ret_val = g_swig_watchpoint_callback (python_function_name, - python_interpreter->m_dictionary_name.c_str(), - stop_frame_sp, - wp_sp); + ret_val = LLDBSwigPythonWatchpointCallbackFunction(python_function_name, python_interpreter->m_dictionary_name.c_str(), + stop_frame_sp, wp_sp); } return ret_val; } @@ -2052,14 +2068,11 @@ if (!implementor) return 0; - if (!g_swig_calc_children) - return 0; - uint32_t ret_val = 0; { Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); - ret_val = g_swig_calc_children (implementor); + ret_val = LLDBSwigPython_CalculateNumChildren(implementor); } return ret_val; @@ -2076,21 +2089,18 @@ if (!implementor) return lldb::ValueObjectSP(); - if (!g_swig_get_child_index || !g_swig_cast_to_sbvalue) - return lldb::ValueObjectSP(); - lldb::ValueObjectSP ret_val; { Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); - void* child_ptr = g_swig_get_child_index (implementor,idx); + void *child_ptr = LLDBSwigPython_GetChildAtIndex(implementor, idx); if (child_ptr != nullptr && child_ptr != Py_None) { - lldb::SBValue* sb_value_ptr = (lldb::SBValue*)g_swig_cast_to_sbvalue(child_ptr); + lldb::SBValue *sb_value_ptr = (lldb::SBValue *)LLDBSWIGPython_CastPyObjectToSBValue(child_ptr); if (sb_value_ptr == nullptr) Py_XDECREF(child_ptr); else - ret_val = g_swig_get_valobj_sp_from_sbvalue (sb_value_ptr); + ret_val = LLDBSWIGPython_GetValueObjectSPFromSBValue(sb_value_ptr); } else { @@ -2112,14 +2122,11 @@ if (!implementor) return UINT32_MAX; - if (!g_swig_get_index_child) - return UINT32_MAX; - int ret_val = UINT32_MAX; { Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); - ret_val = g_swig_get_index_child (implementor, child_name); + ret_val = LLDBSwigPython_GetIndexOfChildWithName(implementor, child_name); } return ret_val; @@ -2138,12 +2145,9 @@ if (!implementor) return ret_val; - if (!g_swig_update_provider) - return ret_val; - { Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); - ret_val = g_swig_update_provider (implementor); + ret_val = LLDBSwigPython_UpdateSynthProviderInstance(implementor); } return ret_val; @@ -2162,12 +2166,9 @@ if (!implementor) return ret_val; - if (!g_swig_mighthavechildren_provider) - return ret_val; - { Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); - ret_val = g_swig_mighthavechildren_provider (implementor); + ret_val = LLDBSwigPython_MightHaveChildrenSynthProviderInstance(implementor); } return ret_val; @@ -2186,25 +2187,20 @@ if (!implementor) return ret_val; - if (!g_swig_getvalue_provider || !g_swig_cast_to_sbvalue || !g_swig_get_valobj_sp_from_sbvalue) - return ret_val; - - { Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); - void* child_ptr = g_swig_getvalue_provider (implementor); + void *child_ptr = LLDBSwigPython_GetValueSynthProviderInstance(implementor); if (child_ptr != nullptr && child_ptr != Py_None) { - lldb::SBValue* sb_value_ptr = (lldb::SBValue*)g_swig_cast_to_sbvalue(child_ptr); + lldb::SBValue *sb_value_ptr = (lldb::SBValue *)LLDBSWIGPython_CastPyObjectToSBValue(child_ptr); if (sb_value_ptr == nullptr) Py_XDECREF(child_ptr); else - ret_val = g_swig_get_valobj_sp_from_sbvalue (sb_value_ptr); + ret_val = LLDBSWIGPython_GetValueObjectSPFromSBValue(sb_value_ptr); } else { Py_XDECREF(child_ptr); } - } return ret_val; } @@ -2212,14 +2208,8 @@ static std::string ReadPythonBacktrace (PyObject* py_backtrace) { - PyObject* traceback_module = nullptr, - *stringIO_module = nullptr, - *stringIO_builder = nullptr, - *stringIO_buffer = nullptr, - *printTB = nullptr, - *printTB_args = nullptr, - *printTB_result = nullptr, - *stringIO_getvalue = nullptr, + PyObject *traceback_module = nullptr, *stringIO_module = nullptr, *stringIO_builder = nullptr, *stringIO_buffer = nullptr, + *printTB = nullptr, *printTB_args = nullptr, *printTB_result = nullptr, *stringIO_getvalue = nullptr, *printTB_string = nullptr; std::string retval("backtrace unavailable"); @@ -2267,10 +2257,7 @@ } bool -ScriptInterpreterPython::RunScriptFormatKeyword (const char* impl_function, - Process* process, - std::string& output, - Error& error) +ScriptInterpreterPython::RunScriptFormatKeyword(const char *impl_function, Process *process, std::string &output, Error &error) { bool ret_val; if (!process) @@ -2283,15 +2270,10 @@ error.SetErrorString("no function to execute"); return false; } - if (!g_swig_run_script_keyword_process) - { - error.SetErrorString("internal helper function missing"); - return false; - } { ProcessSP process_sp(process->shared_from_this()); Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); - ret_val = g_swig_run_script_keyword_process (impl_function, m_dictionary_name.c_str(), process_sp, output); + ret_val = LLDBSWIGPythonRunScriptKeywordProcess(impl_function, m_dictionary_name.c_str(), process_sp, output); if (!ret_val) error.SetErrorString("python script evaluation failed"); } @@ -2299,10 +2281,7 @@ } bool -ScriptInterpreterPython::RunScriptFormatKeyword (const char* impl_function, - Thread* thread, - std::string& output, - Error& error) +ScriptInterpreterPython::RunScriptFormatKeyword(const char *impl_function, Thread *thread, std::string &output, Error &error) { bool ret_val; if (!thread) @@ -2315,15 +2294,10 @@ error.SetErrorString("no function to execute"); return false; } - if (!g_swig_run_script_keyword_thread) - { - error.SetErrorString("internal helper function missing"); - return false; - } { ThreadSP thread_sp(thread->shared_from_this()); Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); - ret_val = g_swig_run_script_keyword_thread (impl_function, m_dictionary_name.c_str(), thread_sp, output); + ret_val = LLDBSWIGPythonRunScriptKeywordThread(impl_function, m_dictionary_name.c_str(), thread_sp, output); if (!ret_val) error.SetErrorString("python script evaluation failed"); } @@ -2331,10 +2305,7 @@ } bool -ScriptInterpreterPython::RunScriptFormatKeyword (const char* impl_function, - Target* target, - std::string& output, - Error& error) +ScriptInterpreterPython::RunScriptFormatKeyword(const char *impl_function, Target *target, std::string &output, Error &error) { bool ret_val; if (!target) @@ -2347,15 +2318,10 @@ error.SetErrorString("no function to execute"); return false; } - if (!g_swig_run_script_keyword_target) - { - error.SetErrorString("internal helper function missing"); - return false; - } { TargetSP target_sp(target->shared_from_this()); Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); - ret_val = g_swig_run_script_keyword_target (impl_function, m_dictionary_name.c_str(), target_sp, output); + ret_val = LLDBSWIGPythonRunScriptKeywordTarget(impl_function, m_dictionary_name.c_str(), target_sp, output); if (!ret_val) error.SetErrorString("python script evaluation failed"); } @@ -2363,10 +2329,7 @@ } bool -ScriptInterpreterPython::RunScriptFormatKeyword (const char* impl_function, - StackFrame* frame, - std::string& output, - Error& error) +ScriptInterpreterPython::RunScriptFormatKeyword(const char *impl_function, StackFrame *frame, std::string &output, Error &error) { bool ret_val; if (!frame) @@ -2379,15 +2342,10 @@ error.SetErrorString("no function to execute"); return false; } - if (!g_swig_run_script_keyword_frame) - { - error.SetErrorString("internal helper function missing"); - return false; - } { StackFrameSP frame_sp(frame->shared_from_this()); Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); - ret_val = g_swig_run_script_keyword_frame (impl_function, m_dictionary_name.c_str(), frame_sp, output); + ret_val = LLDBSWIGPythonRunScriptKeywordFrame(impl_function, m_dictionary_name.c_str(), frame_sp, output); if (!ret_val) error.SetErrorString("python script evaluation failed"); } @@ -2395,10 +2353,7 @@ } bool -ScriptInterpreterPython::RunScriptFormatKeyword (const char* impl_function, - ValueObject *value, - std::string& output, - Error& error) +ScriptInterpreterPython::RunScriptFormatKeyword(const char *impl_function, ValueObject *value, std::string &output, Error &error) { bool ret_val; if (!value) @@ -2411,22 +2366,18 @@ error.SetErrorString("no function to execute"); return false; } - if (!g_swig_run_script_keyword_value) - { - error.SetErrorString("internal helper function missing"); - return false; - } { ValueObjectSP value_sp(value->GetSP()); Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); - ret_val = g_swig_run_script_keyword_value (impl_function, m_dictionary_name.c_str(), value_sp, output); + ret_val = LLDBSWIGPythonRunScriptKeywordValue(impl_function, m_dictionary_name.c_str(), value_sp, output); if (!ret_val) error.SetErrorString("python script evaluation failed"); } return ret_val; } -uint64_t replace_all(std::string& str, const std::string& oldStr, const std::string& newStr) +uint64_t +replace_all(std::string &str, const std::string &oldStr, const std::string &newStr) { size_t pos = 0; uint64_t matches = 0; @@ -2440,10 +2391,7 @@ } bool -ScriptInterpreterPython::LoadScriptingModule (const char* pathname, - bool can_reload, - bool init_session, - lldb_private::Error& error, +ScriptInterpreterPython::LoadScriptingModule(const char *pathname, bool can_reload, bool init_session, lldb_private::Error &error, lldb::ScriptInterpreterObjectSP* module_sp) { if (!pathname || !pathname[0]) @@ -2452,12 +2400,6 @@ return false; } - if (!g_swig_call_module_init) - { - error.SetErrorString("internal helper function missing"); - return false; - } - lldb::DebuggerSP debugger_sp = m_interpreter.GetDebugger().shared_from_this(); { @@ -2467,12 +2409,10 @@ StreamString command_stream; // Before executing Pyton code, lock the GIL. - Locker py_lock (this, - Locker::AcquireLock | (init_session ? Locker::InitSession : 0) | Locker::NoSTDIN, + Locker py_lock(this, Locker::AcquireLock | (init_session ? Locker::InitSession : 0) | Locker::NoSTDIN, Locker::FreeAcquiredLock | (init_session ? Locker::TearDownSession : 0)); - if (target_file.GetFileType() == FileSpec::eFileTypeInvalid || - target_file.GetFileType() == FileSpec::eFileTypeUnknown) + if (target_file.GetFileType() == FileSpec::eFileTypeInvalid || target_file.GetFileType() == FileSpec::eFileTypeUnknown) { // if not a valid file of any sort, check if it might be a filename still // dot can't be used but / and \ can, and if either is found, reject @@ -2483,8 +2423,7 @@ } basename = pathname; // not a filename, probably a package of some sort, let it go through } - else if (target_file.GetFileType() == FileSpec::eFileTypeDirectory || - target_file.GetFileType() == FileSpec::eFileTypeRegular || + else if (target_file.GetFileType() == FileSpec::eFileTypeDirectory || target_file.GetFileType() == FileSpec::eFileTypeRegular || target_file.GetFileType() == FileSpec::eFileTypeSymbolicLink) { std::string directory(target_file.GetDirectory().GetCString()); @@ -2492,10 +2431,11 @@ // now make sure that Python has "directory" in the search path StreamString command_stream; - command_stream.Printf("if not (sys.path.__contains__('%s')):\n sys.path.insert(1,'%s');\n\n", - directory.c_str(), + command_stream.Printf("if not (sys.path.__contains__('%s')):\n sys.path.insert(1,'%s');\n\n", directory.c_str(), directory.c_str()); - bool syspath_retval = ExecuteMultipleLines(command_stream.GetData(), ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false).SetSetLLDBGlobals(false)).Success(); + bool syspath_retval = + ExecuteMultipleLines(command_stream.GetData(), + ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false).SetSetLLDBGlobals(false)).Success(); if (!syspath_retval) { error.SetErrorString("Python sys.path handling failed"); @@ -2524,10 +2464,10 @@ bool does_contain = false; // this call will succeed if the module was ever imported in any Debugger in the lifetime of the process // in which this LLDB framework is living - bool was_imported_globally = (ExecuteOneLineWithReturn(command_stream.GetData(), - ScriptInterpreterPython::eScriptReturnTypeBool, - &does_contain, - ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false).SetSetLLDBGlobals(false)) && does_contain); + bool was_imported_globally = + (ExecuteOneLineWithReturn(command_stream.GetData(), ScriptInterpreterPython::eScriptReturnTypeBool, &does_contain, + ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false).SetSetLLDBGlobals(false)) && + does_contain); // this call will fail if the module was not imported in this Debugger before command_stream.Clear(); command_stream.Printf("sys.getrefcount(%s)",basename.c_str()); @@ -2554,15 +2494,14 @@ else command_stream.Printf("import %s",basename.c_str()); - error = ExecuteMultipleLines(command_stream.GetData(), ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false).SetSetLLDBGlobals(false)); + error = ExecuteMultipleLines(command_stream.GetData(), + ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false).SetSetLLDBGlobals(false)); if (error.Fail()) return false; // if we are here, everything worked // call __lldb_init_module(debugger,dict) - if (!g_swig_call_module_init (basename.c_str(), - m_dictionary_name.c_str(), - debugger_sp)) + if (!LLDBSwigPythonCallModuleInit(basename.c_str(), m_dictionary_name.c_str(), debugger_sp)) { error.SetErrorString("calling __lldb_init_module failed"); return false; @@ -2574,7 +2513,8 @@ command_stream.Clear(); command_stream.Printf("%s",basename.c_str()); void* module_pyobj = nullptr; - if (ExecuteOneLineWithReturn(command_stream.GetData(),ScriptInterpreter::eScriptReturnTypeOpaqueObject,&module_pyobj) && module_pyobj) + if (ExecuteOneLineWithReturn(command_stream.GetData(), ScriptInterpreter::eScriptReturnTypeOpaqueObject, &module_pyobj) && + module_pyobj) *module_sp = MakeScriptObject(module_pyobj); } @@ -2603,11 +2543,10 @@ return lldb::ScriptInterpreterObjectSP(new ScriptInterpreterPythonObject(object)); } -ScriptInterpreterPython::SynchronicityHandler::SynchronicityHandler (lldb::DebuggerSP debugger_sp, - ScriptedCommandSynchronicity synchro) : - m_debugger_sp(debugger_sp), - m_synch_wanted(synchro), - m_old_asynch(debugger_sp->GetAsyncExecution()) +ScriptInterpreterPython::SynchronicityHandler::SynchronicityHandler(lldb::DebuggerSP debugger_sp, ScriptedCommandSynchronicity synchro) + : m_debugger_sp(debugger_sp) + , m_synch_wanted(synchro) + , m_old_asynch(debugger_sp->GetAsyncExecution()) { if (m_synch_wanted == eScriptedCommandSynchronicitySynchronous) m_debugger_sp->SetAsyncExecution(false); @@ -2622,11 +2561,8 @@ } bool -ScriptInterpreterPython::RunScriptBasedCommand(const char* impl_function, - const char* args, - ScriptedCommandSynchronicity synchronicity, - lldb_private::CommandReturnObject& cmd_retobj, - Error& error, +ScriptInterpreterPython::RunScriptBasedCommand(const char *impl_function, const char *args, ScriptedCommandSynchronicity synchronicity, + lldb_private::CommandReturnObject &cmd_retobj, Error &error, const lldb_private::ExecutionContext& exe_ctx) { if (!impl_function) @@ -2635,12 +2571,6 @@ return false; } - if (!g_swig_call_command) - { - error.SetErrorString("no helper function to run scripted commands"); - return false; - } - lldb::DebuggerSP debugger_sp = m_interpreter.GetDebugger().shared_from_this(); lldb::ExecutionContextRefSP exe_ctx_ref_sp(new ExecutionContextRef(exe_ctx)); @@ -2655,19 +2585,12 @@ std::string err_msg; { - Locker py_lock(this, - Locker::AcquireLock | Locker::InitSession | (cmd_retobj.GetInteractive() ? 0 : Locker::NoSTDIN), + Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | (cmd_retobj.GetInteractive() ? 0 : Locker::NoSTDIN), Locker::FreeLock | Locker::TearDownSession); - SynchronicityHandler synch_handler(debugger_sp, - synchronicity); + SynchronicityHandler synch_handler(debugger_sp, synchronicity); - ret_val = g_swig_call_command (impl_function, - m_dictionary_name.c_str(), - debugger_sp, - args, - cmd_retobj, - exe_ctx_ref_sp); + ret_val = LLDBSwigPythonCallCommand(impl_function, m_dictionary_name.c_str(), debugger_sp, args, cmd_retobj, exe_ctx_ref_sp); } if (!ret_val) @@ -2692,9 +2615,7 @@ char* result_ptr = nullptr; // Python is going to point this to valid data if ExecuteOneLineWithReturn returns successfully - if (ExecuteOneLineWithReturn (command.c_str(), - ScriptInterpreter::eScriptReturnTypeCharStrOrNone, - &result_ptr, + if (ExecuteOneLineWithReturn(command.c_str(), ScriptInterpreter::eScriptReturnTypeCharStrOrNone, &result_ptr, ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false))) { if (result_ptr) @@ -2713,65 +2634,12 @@ std::unique_ptr ScriptInterpreterPython::AcquireInterpreterLock () { - std::unique_ptr py_lock(new Locker(this, - Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN, - Locker::FreeLock | Locker::TearDownSession)); + std::unique_ptr py_lock( + new Locker(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN, Locker::FreeLock | Locker::TearDownSession)); return py_lock; } void -ScriptInterpreterPython::InitializeInterpreter (SWIGInitCallback swig_init_callback, - SWIGBreakpointCallbackFunction swig_breakpoint_callback, - SWIGWatchpointCallbackFunction swig_watchpoint_callback, - SWIGPythonTypeScriptCallbackFunction swig_typescript_callback, - SWIGPythonCreateSyntheticProvider swig_synthetic_script, - SWIGPythonCalculateNumChildren swig_calc_children, - SWIGPythonGetChildAtIndex swig_get_child_index, - SWIGPythonGetIndexOfChildWithName swig_get_index_child, - SWIGPythonCastPyObjectToSBValue swig_cast_to_sbvalue , - SWIGPythonGetValueObjectSPFromSBValue swig_get_valobj_sp_from_sbvalue, - SWIGPythonUpdateSynthProviderInstance swig_update_provider, - SWIGPythonMightHaveChildrenSynthProviderInstance swig_mighthavechildren_provider, - SWIGPythonGetValueSynthProviderInstance swig_getvalue_provider, - SWIGPythonCallCommand swig_call_command, - SWIGPythonCallModuleInit swig_call_module_init, - SWIGPythonCreateOSPlugin swig_create_os_plugin, - SWIGPythonScriptKeyword_Process swig_run_script_keyword_process, - SWIGPythonScriptKeyword_Thread swig_run_script_keyword_thread, - SWIGPythonScriptKeyword_Target swig_run_script_keyword_target, - SWIGPythonScriptKeyword_Frame swig_run_script_keyword_frame, - SWIGPythonScriptKeyword_Value swig_run_script_keyword_value, - SWIGPython_GetDynamicSetting swig_plugin_get, - SWIGPythonCreateScriptedThreadPlan swig_thread_plan_script, - SWIGPythonCallThreadPlan swig_call_thread_plan) -{ - g_swig_init_callback = swig_init_callback; - g_swig_breakpoint_callback = swig_breakpoint_callback; - g_swig_watchpoint_callback = swig_watchpoint_callback; - g_swig_typescript_callback = swig_typescript_callback; - g_swig_synthetic_script = swig_synthetic_script; - g_swig_calc_children = swig_calc_children; - g_swig_get_child_index = swig_get_child_index; - g_swig_get_index_child = swig_get_index_child; - g_swig_cast_to_sbvalue = swig_cast_to_sbvalue; - g_swig_get_valobj_sp_from_sbvalue = swig_get_valobj_sp_from_sbvalue; - g_swig_update_provider = swig_update_provider; - g_swig_mighthavechildren_provider = swig_mighthavechildren_provider; - g_swig_getvalue_provider = swig_getvalue_provider; - g_swig_call_command = swig_call_command; - g_swig_call_module_init = swig_call_module_init; - g_swig_create_os_plugin = swig_create_os_plugin; - g_swig_run_script_keyword_process = swig_run_script_keyword_process; - g_swig_run_script_keyword_thread = swig_run_script_keyword_thread; - g_swig_run_script_keyword_target = swig_run_script_keyword_target; - g_swig_run_script_keyword_frame = swig_run_script_keyword_frame; - g_swig_run_script_keyword_value = swig_run_script_keyword_value; - g_swig_plugin_get = swig_plugin_get; - g_swig_thread_plan_script = swig_thread_plan_script; - g_swig_call_thread_plan = swig_call_thread_plan; -} - -void ScriptInterpreterPython::InitializePrivate () { static int g_initialized = false; @@ -2791,20 +2659,22 @@ PyGILState_STATE gstate; Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT | LIBLLDB_LOG_VERBOSE)); bool threads_already_initialized = false; - if (PyEval_ThreadsInitialized ()) { + if (PyEval_ThreadsInitialized()) + { gstate = PyGILState_Ensure (); if (log) log->Printf("Ensured PyGILState. Previous state = %slocked\n", gstate == PyGILState_UNLOCKED ? "un" : ""); threads_already_initialized = true; - } else { + } + else + { // InitThreads acquires the GIL if it hasn't been called before. PyEval_InitThreads (); } Py_InitializeEx (0); // Initialize SWIG after setting up python - if (g_swig_init_callback) - g_swig_init_callback (); + init_lldb(); // Update the path python uses to search for modules to include the current directory. @@ -2847,18 +2717,22 @@ int old_count = Debugger::TestDebuggerRefCount (); - PyRun_SimpleString ("sys.dont_write_bytecode = 1; import lldb.embedded_interpreter; from lldb.embedded_interpreter import run_python_interpreter; from lldb.embedded_interpreter import run_one_line"); + PyRun_SimpleString("sys.dont_write_bytecode = 1; import lldb.embedded_interpreter; from lldb.embedded_interpreter import " + "run_python_interpreter; from lldb.embedded_interpreter import run_one_line"); int new_count = Debugger::TestDebuggerRefCount (); if (new_count > old_count) Debugger::Terminate (); - if (threads_already_initialized) { + if (threads_already_initialized) + { if (log) log->Printf("Releasing PyGILState. Returning to state = %slocked\n", gstate == PyGILState_UNLOCKED ? "un" : ""); PyGILState_Release (gstate); - } else { + } + else + { // We initialized the threads in this function, just unlock the GIL. PyEval_SaveThread(); } Index: source/ScriptInterpreter/ScriptInterpreter.cpp =================================================================== --- source/ScriptInterpreter/ScriptInterpreter.cpp +++ source/ScriptInterpreter/ScriptInterpreter.cpp @@ -9,62 +9,67 @@ #include "lldb/lldb-python.h" -#include "lldb/Interpreter/ScriptInterpreter.h" +#include "lldb/ScriptInterpreter/ScriptInterpreter.h" #include #include #include #include "lldb/Core/Error.h" +#include "lldb/Core/Log.h" #include "lldb/Core/Stream.h" #include "lldb/Core/StringList.h" +#include "lldb/Host/Mutex.h" #include "lldb/Interpreter/CommandReturnObject.h" -#include "lldb/Interpreter/ScriptInterpreterPython.h" +#include "lldb/ScriptInterpreter/ScriptInterpreterNone.h" +#ifndef LLDB_DISABLE_PYTHON +#include "lldb/ScriptInterpreter/Python/ScriptInterpreterPython.h" +#endif #include "lldb/Utility/PseudoTerminal.h" using namespace lldb; using namespace lldb_private; -ScriptInterpreter::ScriptInterpreter (CommandInterpreter &interpreter, lldb::ScriptLanguage script_lang) : - m_interpreter (interpreter), - m_script_lang (script_lang) +namespace +{ +ScriptInterpreterNone *g_none_interpreter = nullptr; +#ifndef LLDB_DISABLE_PYTHON +ScriptInterpreterPython *g_python_interpreter = nullptr; +#endif +} + +ScriptInterpreter::ScriptInterpreter(CommandInterpreter &interpreter, lldb::ScriptLanguage script_lang) + : m_interpreter(interpreter) + , m_script_lang(script_lang) { } -ScriptInterpreter::~ScriptInterpreter () +ScriptInterpreter::~ScriptInterpreter() { } CommandInterpreter & -ScriptInterpreter::GetCommandInterpreter () +ScriptInterpreter::GetCommandInterpreter() { return m_interpreter; } -void -ScriptInterpreter::CollectDataForBreakpointCommandCallback -( - std::vector &bp_options_vec, - CommandReturnObject &result -) +void +ScriptInterpreter::CollectDataForBreakpointCommandCallback(std::vector &bp_options_vec, CommandReturnObject &result) { - result.SetStatus (eReturnStatusFailed); - result.AppendError ("ScriptInterpreter::GetScriptCommands(StringList &) is not implemented."); + result.SetStatus(eReturnStatusFailed); + result.AppendError("ScriptInterpreter::GetScriptCommands(StringList &) is not implemented."); } -void -ScriptInterpreter::CollectDataForWatchpointCommandCallback -( - WatchpointOptions *bp_options, - CommandReturnObject &result -) +void +ScriptInterpreter::CollectDataForWatchpointCommandCallback(WatchpointOptions *bp_options, CommandReturnObject &result) { - result.SetStatus (eReturnStatusFailed); - result.AppendError ("ScriptInterpreter::GetScriptCommands(StringList &) is not implemented."); + result.SetStatus(eReturnStatusFailed); + result.AppendError("ScriptInterpreter::GetScriptCommands(StringList &) is not implemented."); } std::string -ScriptInterpreter::LanguageToString (lldb::ScriptLanguage language) +ScriptInterpreter::LanguageToString(lldb::ScriptLanguage language) { std::string return_value; @@ -82,8 +87,7 @@ } Error -ScriptInterpreter::SetBreakpointCommandCallback (std::vector &bp_options_vec, - const char *callback_text) +ScriptInterpreter::SetBreakpointCommandCallback(std::vector &bp_options_vec, const char *callback_text) { Error return_error; for (BreakpointOptions *bp_options : bp_options_vec) @@ -96,8 +100,7 @@ } void -ScriptInterpreter::SetBreakpointCommandCallbackFunction (std::vector &bp_options_vec, - const char *function_name) +ScriptInterpreter::SetBreakpointCommandCallbackFunction(std::vector &bp_options_vec, const char *function_name) { for (BreakpointOptions *bp_options : bp_options_vec) { @@ -106,61 +109,52 @@ } std::unique_ptr -ScriptInterpreter::AcquireInterpreterLock () +ScriptInterpreter::AcquireInterpreterLock() { return std::unique_ptr(new ScriptInterpreterLocker()); } +ScriptInterpreter * +ScriptInterpreter::GetScriptInterpreter(lldb::ScriptLanguage script_lang, CommandInterpreter &command_interpreter, bool can_create) +{ + // + // we need to protect the initialization of the script interpreter + // otherwise we could end up with two threads both trying to create + // their instance of it, and for some languages (e.g. Python) + // this is a bulletproof recipe for disaster! + // this needs to be a function-level static because multiple Debugger + // instances living in the same process still need to be isolated and + // not try to initialize the script interpreter concurrently. + static Mutex g_interpreter_mutex(Mutex::eMutexTypeRecursive); + Mutex::Locker interpreter_lock(g_interpreter_mutex); + + Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SCRIPT)); + if (log) + log->Printf("Initializing the ScriptInterpreter now\n"); + + switch (script_lang) + { + case eScriptLanguagePython: +#ifndef LLDB_DISABLE_PYTHON + if (!g_python_interpreter && can_create) + g_python_interpreter = new ScriptInterpreterPython(command_interpreter); + return g_python_interpreter; +#else +// Fall through to the None case when python is disabled +#endif + case eScriptLanguageNone: + if (!g_none_interpreter && can_create) + g_none_interpreter = new ScriptInterpreterNone(command_interpreter); + return g_none_interpreter; + }; + + return nullptr; +} + void -ScriptInterpreter::InitializeInterpreter (SWIGInitCallback python_swig_init_callback, - SWIGBreakpointCallbackFunction swig_breakpoint_callback, - SWIGWatchpointCallbackFunction swig_watchpoint_callback, - SWIGPythonTypeScriptCallbackFunction swig_typescript_callback, - SWIGPythonCreateSyntheticProvider swig_synthetic_script, - SWIGPythonCalculateNumChildren swig_calc_children, - SWIGPythonGetChildAtIndex swig_get_child_index, - SWIGPythonGetIndexOfChildWithName swig_get_index_child, - SWIGPythonCastPyObjectToSBValue swig_cast_to_sbvalue , - SWIGPythonGetValueObjectSPFromSBValue swig_get_valobj_sp_from_sbvalue, - SWIGPythonUpdateSynthProviderInstance swig_update_provider, - SWIGPythonMightHaveChildrenSynthProviderInstance swig_mighthavechildren_provider, - SWIGPythonGetValueSynthProviderInstance swig_getvalue_provider, - SWIGPythonCallCommand swig_call_command, - SWIGPythonCallModuleInit swig_call_module_init, - SWIGPythonCreateOSPlugin swig_create_os_plugin, - SWIGPythonScriptKeyword_Process swig_run_script_keyword_process, - SWIGPythonScriptKeyword_Thread swig_run_script_keyword_thread, - SWIGPythonScriptKeyword_Target swig_run_script_keyword_target, - SWIGPythonScriptKeyword_Frame swig_run_script_keyword_frame, - SWIGPythonScriptKeyword_Value swig_run_script_keyword_value, - SWIGPython_GetDynamicSetting swig_plugin_get, - SWIGPythonCreateScriptedThreadPlan swig_thread_plan_script, - SWIGPythonCallThreadPlan swig_call_thread_plan) +ScriptInterpreter::InitializePrivate() { #ifndef LLDB_DISABLE_PYTHON - ScriptInterpreterPython::InitializeInterpreter (python_swig_init_callback, - swig_breakpoint_callback, - swig_watchpoint_callback, - swig_typescript_callback, - swig_synthetic_script, - swig_calc_children, - swig_get_child_index, - swig_get_index_child, - swig_cast_to_sbvalue , - swig_get_valobj_sp_from_sbvalue, - swig_update_provider, - swig_mighthavechildren_provider, - swig_getvalue_provider, - swig_call_command, - swig_call_module_init, - swig_create_os_plugin, - swig_run_script_keyword_process, - swig_run_script_keyword_thread, - swig_run_script_keyword_target, - swig_run_script_keyword_frame, - swig_run_script_keyword_value, - swig_plugin_get, - swig_thread_plan_script, - swig_call_thread_plan); -#endif // #ifndef LLDB_DISABLE_PYTHON + ScriptInterpreterPython::InitializePrivate(); +#endif } Index: source/ScriptInterpreter/ScriptInterpreterNone.cpp =================================================================== --- source/ScriptInterpreter/ScriptInterpreterNone.cpp +++ source/ScriptInterpreter/ScriptInterpreterNone.cpp @@ -7,9 +7,7 @@ // //===----------------------------------------------------------------------===// -#include "lldb/lldb-python.h" - -#include "lldb/Interpreter/ScriptInterpreterNone.h" +#include "lldb/ScriptInterpreter/ScriptInterpreterNone.h" #include "lldb/Core/Stream.h" #include "lldb/Core/StreamFile.h" #include "lldb/Core/StringList.h" @@ -19,26 +17,24 @@ using namespace lldb; using namespace lldb_private; -ScriptInterpreterNone::ScriptInterpreterNone (CommandInterpreter &interpreter) : - ScriptInterpreter (interpreter, eScriptLanguageNone) +ScriptInterpreterNone::ScriptInterpreterNone(CommandInterpreter &interpreter) + : ScriptInterpreter(interpreter, eScriptLanguageNone) { } -ScriptInterpreterNone::~ScriptInterpreterNone () +ScriptInterpreterNone::~ScriptInterpreterNone() { } bool -ScriptInterpreterNone::ExecuteOneLine (const char *command, CommandReturnObject *, const ExecuteScriptOptions&) +ScriptInterpreterNone::ExecuteOneLine(const char *command, CommandReturnObject *, const ExecuteScriptOptions &) { - m_interpreter.GetDebugger().GetErrorFile()->PutCString ("error: there is no embedded script interpreter in this mode.\n"); + m_interpreter.GetDebugger().GetErrorFile()->PutCString("error: there is no embedded script interpreter in this mode.\n"); return false; } void -ScriptInterpreterNone::ExecuteInterpreterLoop () +ScriptInterpreterNone::ExecuteInterpreterLoop() { - m_interpreter.GetDebugger().GetErrorFile()->PutCString ("error: there is no embedded script interpreter in this mode.\n"); + m_interpreter.GetDebugger().GetErrorFile()->PutCString("error: there is no embedded script interpreter in this mode.\n"); } - - Index: source/Target/ThreadPlanPython.cpp =================================================================== --- source/Target/ThreadPlanPython.cpp +++ source/Target/ThreadPlanPython.cpp @@ -19,8 +19,7 @@ #include "lldb/Core/Log.h" #include "lldb/Core/State.h" #include "lldb/Interpreter/CommandInterpreter.h" -#include "lldb/Interpreter/ScriptInterpreter.h" -#include "lldb/Interpreter/ScriptInterpreterPython.h" +#include "lldb/ScriptInterpreter/ScriptInterpreter.h" #include "lldb/Target/RegisterContext.h" #include "lldb/Target/Thread.h" #include "lldb/Target/ThreadPlan.h" Index: source/lldb.cpp =================================================================== --- source/lldb.cpp +++ source/lldb.cpp @@ -20,7 +20,7 @@ #include "lldb/Host/Host.h" #include "lldb/Host/HostInfo.h" #include "lldb/Host/Mutex.h" -#include "lldb/Interpreter/ScriptInterpreterPython.h" +#include "lldb/ScriptInterpreter/ScriptInterpreter.h" #include "lldb/Target/Target.h" #include "lldb/Target/Thread.h" @@ -177,8 +177,8 @@ PlatformAndroid::Initialize(); SymbolFileDWARFDebugMap::Initialize(); ItaniumABILanguageRuntime::Initialize(); + ScriptInterpreter::InitializePrivate(); #ifndef LLDB_DISABLE_PYTHON - ScriptInterpreterPython::InitializePrivate(); OperatingSystemPython::Initialize(); #endif JITLoaderGDB::Initialize(); Index: tools/driver/CMakeLists.txt =================================================================== --- tools/driver/CMakeLists.txt +++ tools/driver/CMakeLists.txt @@ -2,6 +2,8 @@ add_lldb_executable(lldb Driver.cpp Platform.cpp + ADDITIONAL_HEADER_DIRS + ${LLDB_INCLUDE_ROOT}/lldb ) if ( CMAKE_SYSTEM_NAME MATCHES "Windows" ) Index: tools/lldb-server/CMakeLists.txt =================================================================== --- tools/lldb-server/CMakeLists.txt +++ tools/lldb-server/CMakeLists.txt @@ -2,19 +2,19 @@ if ( CMAKE_SYSTEM_NAME MATCHES "Linux" ) include_directories( - ../../source/Plugins/Process/Linux - ../../source/Plugins/Process/POSIX + ${LLDB_SOURCE_ROOT}/Plugins/Process/Linux + ${LLDB_SOURCE_ROOT}/Plugins/Process/POSIX ) endif () if ( CMAKE_SYSTEM_NAME MATCHES "FreeBSD" ) include_directories( - ../../source/Plugins/Process/FreeBSD - ../../source/Plugins/Process/POSIX + ${LLDB_SOURCE_ROOT}/Plugins/Process/FreeBSD + ${LLDB_SOURCE_ROOT}/Plugins/Process/POSIX ) endif () -include_directories(../../source) +include_directories(${LLDB_SOURCE_ROOT}) include(../../cmake/LLDBDependencies.cmake) @@ -31,8 +31,10 @@ lldb-server.cpp lldb-gdbserver.cpp lldb-platform.cpp - ../../source/lldb-log.cpp - ../../source/lldb.cpp + ${LLDB_SOURCE_ROOT}/lldb-log.cpp + ${LLDB_SOURCE_ROOT}/lldb.cpp + $ + ${LLDB_WRAP_PYTHON} ) # The Darwin linker doesn't understand --start-group/--end-group.