| 1 |
#include "BreakpointsHelper.hpp" |
| 2 |
|
| 3 |
#include "event_notifier.h" |
| 4 |
|
| 5 |
#include <algorithm> |
| 6 |
|
| 7 |
namespace |
| 8 |
{ |
| 9 |
// helper methods converting between clDebuggerBreakpoint and dap::XXXBreakpoint |
| 10 |
bool is_source_breakpoint(const clDebuggerBreakpoint& bp) |
| 11 |
{ |
| 12 |
return bp.bp_type == BreakpointType::BP_type_break && !bp.file.empty() && bp.lineno > 0; |
| 13 |
} |
| 14 |
|
| 15 |
bool is_function_breakpoint(const clDebuggerBreakpoint& bp) |
| 16 |
{ |
| 17 |
return bp.bp_type == BreakpointType::BP_type_break && !bp.function_name.empty(); |
| 18 |
} |
| 19 |
|
| 20 |
dap::SourceBreakpoint to_dap_source_bp(const clDebuggerBreakpoint& bp) |
| 21 |
{ |
| 22 |
dap::SourceBreakpoint d; |
| 23 |
d.line = bp.lineno; |
| 24 |
d.condition = bp.conditions; |
| 25 |
return d; |
| 26 |
} |
| 27 |
|
| 28 |
dap::FunctionBreakpoint to_dap_function_bp(const clDebuggerBreakpoint& bp) |
| 29 |
{ |
| 30 |
dap::FunctionBreakpoint d; |
| 31 |
d.name = bp.function_name; |
| 32 |
d.condition = bp.conditions; |
| 33 |
return d; |
| 34 |
} |
| 35 |
|
| 36 |
} // namespace |
| 37 |
|
| 38 |
BreakpointsHelper::BreakpointsHelper(dap::Client& client, const DebugSession& session, clModuleLogger& log) |
| 39 |
: m_client(client) |
| 40 |
, m_session(session) |
| 41 |
, LOG(log) |
| 42 |
{ |
| 43 |
// create a snapshot of the breakpoints manager |
| 44 |
clDebuggerBreakpoint::Vec_t all_bps; |
| 45 |
clGetManager()->GetAllBreakpoints(all_bps); |
| 46 |
|
| 47 |
for(const auto& bp : all_bps) { |
| 48 |
if(bp.file.empty() || bp.lineno <= 0) |
| 49 |
continue; |
| 50 |
|
| 51 |
if(m_ui_breakpoints.count(bp.file) == 0) { |
| 52 |
m_ui_breakpoints.insert({ bp.file, {} }); |
| 53 |
} |
| 54 |
m_ui_breakpoints[bp.file].push_back(bp); |
| 55 |
} |
| 56 |
|
| 57 |
clGetManager()->DeleteAllBreakpoints(); |
| 58 |
EventNotifier::Get()->Bind(wxEVT_DBG_UI_TOGGLE_BREAKPOINT, &BreakpointsHelper::OnToggleBreakpoint, this); |
| 59 |
} |
| 60 |
|
| 61 |
BreakpointsHelper::~BreakpointsHelper() |
| 62 |
{ |
| 63 |
// restore the breakpoints |
| 64 |
clDebuggerBreakpoint::Vec_t all_bps; |
| 65 |
for(const auto& vt : m_ui_breakpoints) { |
| 66 |
LOG_DEBUG(LOG) << "Restoring breakpoints for file:" << vt.first << " -" << vt.second.size() << "breakpoints" |
| 67 |
<< endl; |
| 68 |
for(const auto& bp : vt.second) { |
| 69 |
all_bps.push_back(bp); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
clGetManager()->SetBreakpoints(all_bps); |
| 74 |
LOG_DEBUG(LOG) << "Restoring breakpoints...done" << endl; |
| 75 |
|
| 76 |
// refresh markers |
| 77 |
EventNotifier::Get()->Unbind(wxEVT_DBG_UI_TOGGLE_BREAKPOINT, &BreakpointsHelper::OnToggleBreakpoint, this); |
| 78 |
} |
| 79 |
|
| 80 |
void BreakpointsHelper::OnToggleBreakpoint(clDebugEvent& event) |
| 81 |
{ |
| 82 |
// this instance only exists while an active debug session is running |
| 83 |
// during such a session, we capture all the UI breakpoints adding/deleting |
| 84 |
event.Skip(); |
| 85 |
LOG_DEBUG(LOG) << "Toggle breakpoint called for:" << event.GetFileName() << ":" << event.GetLineNumber() << endl; |
| 86 |
if(m_ui_breakpoints.count(event.GetFileName()) == 0) { |
| 87 |
m_ui_breakpoints.insert({ event.GetFileName(), {} }); |
| 88 |
} |
| 89 |
|
| 90 |
auto& file_breakpoints = m_ui_breakpoints[event.GetFileName()]; |
| 91 |
auto iter = std::find_if(file_breakpoints.begin(), file_breakpoints.end(), [&](const clDebuggerBreakpoint& bp) { |
| 92 |
return bp.file == event.GetFileName() && bp.lineno == event.GetLineNumber(); |
| 93 |
}); |
| 94 |
|
| 95 |
if(iter == file_breakpoints.end()) { |
| 96 |
auto bp = clGetManager()->CreateBreakpoint(event.GetFileName(), event.GetLineNumber()); |
| 97 |
file_breakpoints.push_back(bp); |
| 98 |
LOG_DEBUG(LOG) << "Breakpoint does not exist - adding it" << endl; |
| 99 |
} else { |
| 100 |
file_breakpoints.erase(iter); |
| 101 |
LOG_DEBUG(LOG) << "Breakpoint exists - deleting it" << endl; |
| 102 |
} |
| 103 |
|
| 104 |
// need to apply the breakpoints for this file |
| 105 |
ApplyBreakpoints(event.GetFileName()); |
| 106 |
} |
| 107 |
|
| 108 |
void BreakpointsHelper::ApplyBreakpoints(const wxString& path) |
| 109 |
{ |
| 110 |
if(!m_client.IsConnected()) { |
| 111 |
return; |
| 112 |
} |
| 113 |
LOG_DEBUG(LOG) << "Applying breakpoints" << endl; |
| 114 |
std::unordered_map<wxString, std::vector<dap::SourceBreakpoint>> dap_source_breakpoints; |
| 115 |
std::vector<dap::FunctionBreakpoint> dap_function_breakpoints; |
| 116 |
|
| 117 |
if(m_ui_breakpoints.empty()) { |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
dap_source_breakpoints.reserve(m_ui_breakpoints.size()); |
| 122 |
dap_function_breakpoints.reserve(m_ui_breakpoints.size()); |
| 123 |
|
| 124 |
for(const auto& vt : m_ui_breakpoints) { |
| 125 |
if(vt.second.empty()) { |
| 126 |
// no breakpoints for this source file, we need to pass an empty array to dap |
| 127 |
dap_source_breakpoints.insert({ vt.first, {} }); |
| 128 |
|
| 129 |
} else { |
| 130 |
for(const auto& bp : vt.second) { |
| 131 |
if(!is_source_breakpoint(bp)) { |
| 132 |
continue; |
| 133 |
} |
| 134 |
if(path.empty() || path == bp.file) { |
| 135 |
// all breakpoints |
| 136 |
if(dap_source_breakpoints.count(bp.file) == 0) { |
| 137 |
dap_source_breakpoints.insert({ bp.file, {} }); |
| 138 |
} |
| 139 |
dap_source_breakpoints[bp.file].push_back(to_dap_source_bp(bp)); |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
// dont pass empty array, it will tell dap to clear all breakpoints |
| 146 |
for(const auto& vt : dap_source_breakpoints) { |
| 147 |
wxFileName filepath(vt.first); |
| 148 |
LOG_DEBUG(LOG) << "Applying breakpoints for file:" << filepath << endl; |
| 149 |
for(const auto& bp : vt.second) { |
| 150 |
LOG_DEBUG(LOG) << "Line:" << bp.line << ". Condition: " << bp.condition << endl; |
| 151 |
} |
| 152 |
wxString source_path = NormalisePathForSend(filepath.GetFullPath()); |
| 153 |
m_client.SetBreakpointsFile(source_path, vt.second); |
| 154 |
} |
| 155 |
|
| 156 |
if(!dap_function_breakpoints.empty()) { |
| 157 |
LOG_DEBUG(LOG) << "Applying function breakpoints:" << endl; |
| 158 |
for(const auto& bp : dap_function_breakpoints) { |
| 159 |
LOG_DEBUG(LOG) << "Function name:" << bp.name << ". Condition: " << bp.condition << endl; |
| 160 |
} |
| 161 |
m_client.SetFunctionBreakpoints(dap_function_breakpoints); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
wxString BreakpointsHelper::NormalisePathForSend(const wxString& path) const |
| 166 |
{ |
| 167 |
wxFileName fn(path); |
| 168 |
|
| 169 |
// easy path |
| 170 |
if(m_session.dap_server.UseRelativePath()) { |
| 171 |
return fn.GetFullName(); |
| 172 |
} |
| 173 |
|
| 174 |
const auto& dap = m_session.dap_server; |
| 175 |
|
| 176 |
// attempt to make it fullpath |
| 177 |
if(fn.IsRelative() && !dap.UseRelativePath()) { |
| 178 |
fn.MakeAbsolute(m_session.working_directory); |
| 179 |
} |
| 180 |
|
| 181 |
if(!dap.UseVolume()) { |
| 182 |
// no volume |
| 183 |
fn.SetVolume(wxEmptyString); |
| 184 |
} |
| 185 |
|
| 186 |
wxString fullpath = fn.GetFullPath(); |
| 187 |
if(dap.UseForwardSlash()) { |
| 188 |
fullpath.Replace(R"(\)", "/"); |
| 189 |
} |
| 190 |
return fullpath; |
| 191 |
} |