diff --git a/src/debug_lldb.cc b/src/debug_lldb.cc index 925fe48..812f81f 100644 --- a/src/debug_lldb.cc +++ b/src/debug_lldb.cc @@ -12,6 +12,8 @@ extern char **environ; +bool Debug::LLDB::initialized = false; + void log(const char *msg, void *) { std::cout << "debugger log: " << msg << std::endl; } @@ -31,6 +33,21 @@ Debug::LLDB::LLDB() : state(lldb::StateType::eStateInvalid), buffer_size(131072) } } +void Debug::LLDB::destroy_() { + { + LockGuard lock(mutex); + if(process) + process->Kill(); + } + + if(debug_thread.joinable()) + debug_thread.join(); + + LockGuard lock(mutex); + if(debugger) + lldb::SBDebugger::Destroy(*debugger); +} + std::tuple, std::string, std::vector> Debug::LLDB::parse_run_arguments(const std::string &command) { std::vector environment; std::string executable; @@ -89,6 +106,7 @@ void Debug::LLDB::start(const std::string &command, const boost::filesystem::pat LockGuard lock(mutex); if(!debugger) { + initialized = true; lldb::SBDebugger::Initialize(); debugger = std::make_unique(lldb::SBDebugger::Create(true, log, nullptr)); listener = std::make_unique("juCi++ lldb listener"); @@ -411,12 +429,6 @@ void Debug::LLDB::select_frame(uint32_t frame_index, uint32_t thread_index_id) { } } -void Debug::LLDB::cancel() { - kill(); - if(debug_thread.joinable()) - debug_thread.join(); -} - std::string Debug::LLDB::get_value(const std::string &variable, const boost::filesystem::path &file_path, unsigned int line_nr, unsigned int line_index) { LockGuard lock(mutex); if(state == lldb::StateType::eStateStopped) { diff --git a/src/debug_lldb.h b/src/debug_lldb.h index 43fa856..2824d79 100644 --- a/src/debug_lldb.h +++ b/src/debug_lldb.h @@ -32,6 +32,9 @@ namespace Debug { private: LLDB(); + void destroy_(); + + static bool initialized; public: static LLDB &get() { @@ -39,6 +42,12 @@ namespace Debug { return singleton; } + /// Must be called before application terminates (cannot be placed in destructor sadly) + static void destroy() { + if(initialized) + get().destroy_(); + } + std::list> on_start; /// The handlers are not run in the main loop. std::list> on_exit; @@ -61,8 +70,6 @@ namespace Debug { std::vector get_variables(); void select_frame(uint32_t frame_index, uint32_t thread_index_id = 0); - void cancel(); - /// Get value using variable name and location std::string get_value(const std::string &variable_name, const boost::filesystem::path &file_path, unsigned int line_nr, unsigned int line_index); /// Get value from expression diff --git a/src/project.cc b/src/project.cc index c4427d6..8b6d727 100644 --- a/src/project.cc +++ b/src/project.cc @@ -663,10 +663,6 @@ bool Project::LLDB::debug_is_running() { void Project::LLDB::debug_write(const std::string &buffer) { Debug::LLDB::get().write(buffer); } - -void Project::LLDB::debug_cancel() { - Debug::LLDB::get().cancel(); -} #endif void Project::LanguageProtocol::show_symbols() { diff --git a/src/project.h b/src/project.h index 602f8b9..0d5c3d0 100644 --- a/src/project.h +++ b/src/project.h @@ -74,7 +74,6 @@ namespace Project { virtual void debug_remove_breakpoint(const boost::filesystem::path &file_path, int line_nr, int line_count) {} virtual bool debug_is_running() { return false; } virtual void debug_write(const std::string &buffer) {} - virtual void debug_cancel() {} }; class LLDB : public virtual Base { @@ -98,7 +97,6 @@ namespace Project { void debug_remove_breakpoint(const boost::filesystem::path &file_path, int line_nr, int line_count) override; bool debug_is_running() override; void debug_write(const std::string &buffer) override; - void debug_cancel() override; #endif }; diff --git a/src/window.cc b/src/window.cc index 22914bb..8989038 100644 --- a/src/window.cc +++ b/src/window.cc @@ -1,5 +1,6 @@ #include "window.h" #include "config.h" +#include "debug_lldb.h" #include "dialogs.h" #include "directories.h" #include "entrybox.h" @@ -1443,9 +1444,9 @@ bool Window::on_delete_event(GdkEventAny *event) { return true; } Terminal::get().kill_async_processes(); + #ifdef JUCI_ENABLE_DEBUG - if(Project::current) - Project::current->debug_cancel(); + Debug::LLDB::destroy(); #endif return false; diff --git a/tests/lldb_test.cc b/tests/lldb_test.cc index 9ab9d6b..ba52adb 100644 --- a/tests/lldb_test.cc +++ b/tests/lldb_test.cc @@ -136,5 +136,5 @@ int main() { std::this_thread::sleep_for(std::chrono::milliseconds(10)); } - Debug::LLDB::get().cancel(); + Debug::LLDB::destroy(); }