From a06bfcb3a82c2947f840f31cbf6e304bbfe9e1ad Mon Sep 17 00:00:00 2001 From: eidheim Date: Tue, 14 Nov 2017 07:14:18 +0100 Subject: [PATCH] Further cleanup of debug lldb events --- src/debug_lldb.cc | 14 +++++++------- src/debug_lldb.h | 8 ++++---- src/project.cc | 16 ++++++++++++---- tests/lldb_test.cc | 8 ++++---- 4 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/debug_lldb.cc b/src/debug_lldb.cc index 3c1fcca..f6af9ea 100644 --- a/src/debug_lldb.cc +++ b/src/debug_lldb.cc @@ -111,7 +111,7 @@ void Debug::LLDB::start(const std::string &command, const boost::filesystem::pat if(!target.IsValid()) { Terminal::get().async_print("Error (debug): Could not create debug target to: "+executable+'\n', true); for(auto &handler: on_exit) - handler.second(-1); + handler(-1); return; } @@ -120,7 +120,7 @@ void Debug::LLDB::start(const std::string &command, const boost::filesystem::pat if(!(target.BreakpointCreateByLocation(breakpoint.first.string().c_str(), breakpoint.second)).IsValid()) { Terminal::get().async_print("Error (debug): Could not create breakpoint at: "+breakpoint.first.string()+":"+std::to_string(breakpoint.second)+'\n', true); for(auto &handler: on_exit) - handler.second(-1); + handler(-1); return; } } @@ -132,7 +132,7 @@ void Debug::LLDB::start(const std::string &command, const boost::filesystem::pat if(error.Fail()) { Terminal::get().async_print(std::string("Error (debug): ")+error.GetCString()+'\n', true); for(auto &handler: on_exit) - handler.second(-1); + handler(-1); return; } lldb::SBEvent event; @@ -174,13 +174,13 @@ void Debug::LLDB::start(const std::string &command, const boost::filesystem::pat if(error.Fail()) { Terminal::get().async_print(std::string("Error (debug): ")+error.GetCString()+'\n', true); for(auto &handler: on_exit) - handler.second(-1); + handler(-1); return; } if(debug_thread.joinable()) debug_thread.join(); for(auto &handler: on_start) - handler.second(*process); + handler(*process); debug_thread=std::thread([this]() { lldb::SBEvent event; while(true) { @@ -202,14 +202,14 @@ void Debug::LLDB::start(const std::string &command, const boost::filesystem::pat lock.unlock(); for(auto &handler: on_event) - handler.second(event); + handler(event); lock.lock(); if(state==lldb::StateType::eStateExited || state==lldb::StateType::eStateCrashed) { auto exit_status=state==lldb::StateType::eStateCrashed?-1:process->GetExitStatus(); lock.unlock(); for(auto &handler: on_exit) - handler.second(exit_status); + handler(exit_status); lock.lock(); process.reset(); this->state=lldb::StateType::eStateInvalid; diff --git a/src/debug_lldb.h b/src/debug_lldb.h index b36f4a5..7716518 100644 --- a/src/debug_lldb.h +++ b/src/debug_lldb.h @@ -2,7 +2,7 @@ #define JUCI_DEBUG_CLANG_H_ #include -#include +#include #include #include #include @@ -39,11 +39,11 @@ namespace Debug { return singleton; } - std::unordered_map> on_start; + std::list> on_start; /// The handlers are not run in the main loop. - std::unordered_map> on_exit; + std::list> on_exit; /// The handlers are not run in the main loop. - std::unordered_map> on_event; + std::list> on_event; std::mutex mutex; diff --git a/src/project.cc b/src/project.cc index 63118a1..e52c1ab 100644 --- a/src/project.cc +++ b/src/project.cc @@ -448,15 +448,22 @@ void Project::Clang::debug_start() { if(options_it!=debug_options.end() && options_it->second.remote_enabled.get_active()) remote_host=options_it->second.remote_host.get_text(); - Debug::LLDB::get().on_exit["debug_start"]=[this, run_arguments](int exit_status) { + static auto on_exit_it=Debug::LLDB::get().on_exit.end(); + if(on_exit_it!=Debug::LLDB::get().on_exit.end()) + Debug::LLDB::get().on_exit.erase(on_exit_it); + Debug::LLDB::get().on_exit.emplace_back([this, run_arguments](int exit_status) { debugging=false; Terminal::get().async_print(*run_arguments+" returned: "+std::to_string(exit_status)+'\n'); dispatcher.post([this] { debug_update_status(""); }); - }; + }); + on_exit_it=std::prev(Debug::LLDB::get().on_exit.end()); - Debug::LLDB::get().on_event["debug_start"]=[this](const lldb::SBEvent &event) { + static auto on_event_it=Debug::LLDB::get().on_event.end(); + if(on_event_it!=Debug::LLDB::get().on_event.end()) + Debug::LLDB::get().on_event.erase(on_event_it); + Debug::LLDB::get().on_event.emplace_back([this](const lldb::SBEvent &event) { std::string status; boost::filesystem::path stop_path; unsigned stop_line=0, stop_column=0; @@ -501,7 +508,8 @@ void Project::Clang::debug_start() { if(auto view=Notebook::get().get_current_view()) view->get_buffer()->place_cursor(view->get_buffer()->get_insert()->get_iter()); }); - }; + }); + on_event_it=std::prev(Debug::LLDB::get().on_event.end()); Debug::LLDB::get().start(*run_arguments, *project_path, breakpoints, remote_host); }); diff --git a/tests/lldb_test.cc b/tests/lldb_test.cc index e6cc4a5..dd86b0f 100644 --- a/tests/lldb_test.cc +++ b/tests/lldb_test.cc @@ -80,11 +80,11 @@ int main() { std::atomic exited(false); int exit_status; std::atomic line_nr(0); - Debug::LLDB::get().on_exit["test"]=[&](int exit_status_) { + Debug::LLDB::get().on_exit.emplace_back([&](int exit_status_) { exit_status=exit_status_; exited=true; - }; - Debug::LLDB::get().on_event["test"]=[&](const lldb::SBEvent &event) { + }); + Debug::LLDB::get().on_event.emplace_back([&](const lldb::SBEvent &event) { std::unique_lock lock(Debug::LLDB::get().mutex); auto process=lldb::SBProcess::GetProcessFromEvent(event); auto state=lldb::SBProcess::GetStateFromEvent(event); @@ -96,7 +96,7 @@ int main() { line_nr=line_entry.GetLine(); } } - }; + }); std::thread debug_thread([&] { Debug::LLDB::get().start(exec_path.string(), "", breakpoints);