Browse Source

Further cleanup of debug lldb events

merge-requests/365/head
eidheim 8 years ago
parent
commit
a06bfcb3a8
  1. 14
      src/debug_lldb.cc
  2. 8
      src/debug_lldb.h
  3. 16
      src/project.cc
  4. 8
      tests/lldb_test.cc

14
src/debug_lldb.cc

@ -111,7 +111,7 @@ void Debug::LLDB::start(const std::string &command, const boost::filesystem::pat
if(!target.IsValid()) { if(!target.IsValid()) {
Terminal::get().async_print("Error (debug): Could not create debug target to: "+executable+'\n', true); Terminal::get().async_print("Error (debug): Could not create debug target to: "+executable+'\n', true);
for(auto &handler: on_exit) for(auto &handler: on_exit)
handler.second(-1); handler(-1);
return; 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()) { 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); 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) for(auto &handler: on_exit)
handler.second(-1); handler(-1);
return; return;
} }
} }
@ -132,7 +132,7 @@ void Debug::LLDB::start(const std::string &command, const boost::filesystem::pat
if(error.Fail()) { if(error.Fail()) {
Terminal::get().async_print(std::string("Error (debug): ")+error.GetCString()+'\n', true); Terminal::get().async_print(std::string("Error (debug): ")+error.GetCString()+'\n', true);
for(auto &handler: on_exit) for(auto &handler: on_exit)
handler.second(-1); handler(-1);
return; return;
} }
lldb::SBEvent event; lldb::SBEvent event;
@ -174,13 +174,13 @@ void Debug::LLDB::start(const std::string &command, const boost::filesystem::pat
if(error.Fail()) { if(error.Fail()) {
Terminal::get().async_print(std::string("Error (debug): ")+error.GetCString()+'\n', true); Terminal::get().async_print(std::string("Error (debug): ")+error.GetCString()+'\n', true);
for(auto &handler: on_exit) for(auto &handler: on_exit)
handler.second(-1); handler(-1);
return; return;
} }
if(debug_thread.joinable()) if(debug_thread.joinable())
debug_thread.join(); debug_thread.join();
for(auto &handler: on_start) for(auto &handler: on_start)
handler.second(*process); handler(*process);
debug_thread=std::thread([this]() { debug_thread=std::thread([this]() {
lldb::SBEvent event; lldb::SBEvent event;
while(true) { while(true) {
@ -202,14 +202,14 @@ void Debug::LLDB::start(const std::string &command, const boost::filesystem::pat
lock.unlock(); lock.unlock();
for(auto &handler: on_event) for(auto &handler: on_event)
handler.second(event); handler(event);
lock.lock(); lock.lock();
if(state==lldb::StateType::eStateExited || state==lldb::StateType::eStateCrashed) { if(state==lldb::StateType::eStateExited || state==lldb::StateType::eStateCrashed) {
auto exit_status=state==lldb::StateType::eStateCrashed?-1:process->GetExitStatus(); auto exit_status=state==lldb::StateType::eStateCrashed?-1:process->GetExitStatus();
lock.unlock(); lock.unlock();
for(auto &handler: on_exit) for(auto &handler: on_exit)
handler.second(exit_status); handler(exit_status);
lock.lock(); lock.lock();
process.reset(); process.reset();
this->state=lldb::StateType::eStateInvalid; this->state=lldb::StateType::eStateInvalid;

8
src/debug_lldb.h

@ -2,7 +2,7 @@
#define JUCI_DEBUG_CLANG_H_ #define JUCI_DEBUG_CLANG_H_
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include <unordered_map> #include <list>
#include <lldb/API/LLDB.h> #include <lldb/API/LLDB.h>
#include <thread> #include <thread>
#include <mutex> #include <mutex>
@ -39,11 +39,11 @@ namespace Debug {
return singleton; return singleton;
} }
std::unordered_map<std::string, std::function<void(const lldb::SBProcess &)>> on_start; std::list<std::function<void(const lldb::SBProcess &)>> on_start;
/// The handlers are not run in the main loop. /// The handlers are not run in the main loop.
std::unordered_map<std::string, std::function<void(int exit_status)>> on_exit; std::list<std::function<void(int exit_status)>> on_exit;
/// The handlers are not run in the main loop. /// The handlers are not run in the main loop.
std::unordered_map<std::string, std::function<void(const lldb::SBEvent &)>> on_event; std::list<std::function<void(const lldb::SBEvent &)>> on_event;
std::mutex mutex; std::mutex mutex;

16
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()) if(options_it!=debug_options.end() && options_it->second.remote_enabled.get_active())
remote_host=options_it->second.remote_host.get_text(); 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; debugging=false;
Terminal::get().async_print(*run_arguments+" returned: "+std::to_string(exit_status)+'\n'); Terminal::get().async_print(*run_arguments+" returned: "+std::to_string(exit_status)+'\n');
dispatcher.post([this] { dispatcher.post([this] {
debug_update_status(""); 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; std::string status;
boost::filesystem::path stop_path; boost::filesystem::path stop_path;
unsigned stop_line=0, stop_column=0; unsigned stop_line=0, stop_column=0;
@ -501,7 +508,8 @@ void Project::Clang::debug_start() {
if(auto view=Notebook::get().get_current_view()) if(auto view=Notebook::get().get_current_view())
view->get_buffer()->place_cursor(view->get_buffer()->get_insert()->get_iter()); 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); Debug::LLDB::get().start(*run_arguments, *project_path, breakpoints, remote_host);
}); });

8
tests/lldb_test.cc

@ -80,11 +80,11 @@ int main() {
std::atomic<bool> exited(false); std::atomic<bool> exited(false);
int exit_status; int exit_status;
std::atomic<int> line_nr(0); std::atomic<int> 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_; exit_status=exit_status_;
exited=true; 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<std::mutex> lock(Debug::LLDB::get().mutex); std::unique_lock<std::mutex> lock(Debug::LLDB::get().mutex);
auto process=lldb::SBProcess::GetProcessFromEvent(event); auto process=lldb::SBProcess::GetProcessFromEvent(event);
auto state=lldb::SBProcess::GetStateFromEvent(event); auto state=lldb::SBProcess::GetStateFromEvent(event);
@ -96,7 +96,7 @@ int main() {
line_nr=line_entry.GetLine(); line_nr=line_entry.GetLine();
} }
} }
}; });
std::thread debug_thread([&] { std::thread debug_thread([&] {
Debug::LLDB::get().start(exec_path.string(), "", breakpoints); Debug::LLDB::get().start(exec_path.string(), "", breakpoints);

Loading…
Cancel
Save