File: BreakpointsHelper.cpp

package info (click to toggle)
codelite 17.0.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 136,244 kB
  • sloc: cpp: 491,547; ansic: 280,393; php: 10,259; sh: 8,930; lisp: 7,664; vhdl: 6,518; python: 6,020; lex: 4,920; yacc: 3,123; perl: 2,385; javascript: 1,715; cs: 1,193; xml: 1,110; makefile: 804; cobol: 741; sql: 709; ruby: 620; f90: 566; ada: 534; asm: 464; fortran: 350; objc: 289; tcl: 258; java: 157; erlang: 61; pascal: 51; ml: 49; awk: 44; haskell: 36
file content (191 lines) | stat: -rw-r--r-- 6,324 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "BreakpointsHelper.hpp"

#include "event_notifier.h"

#include <algorithm>

namespace
{
// helper methods converting between clDebuggerBreakpoint and dap::XXXBreakpoint
bool is_source_breakpoint(const clDebuggerBreakpoint& bp)
{
    return bp.bp_type == BreakpointType::BP_type_break && !bp.file.empty() && bp.lineno > 0;
}

bool is_function_breakpoint(const clDebuggerBreakpoint& bp)
{
    return bp.bp_type == BreakpointType::BP_type_break && !bp.function_name.empty();
}

dap::SourceBreakpoint to_dap_source_bp(const clDebuggerBreakpoint& bp)
{
    dap::SourceBreakpoint d;
    d.line = bp.lineno;
    d.condition = bp.conditions;
    return d;
}

dap::FunctionBreakpoint to_dap_function_bp(const clDebuggerBreakpoint& bp)
{
    dap::FunctionBreakpoint d;
    d.name = bp.function_name;
    d.condition = bp.conditions;
    return d;
}

} // namespace

BreakpointsHelper::BreakpointsHelper(dap::Client& client, const DebugSession& session, clModuleLogger& log)
    : m_client(client)
    , m_session(session)
    , LOG(log)
{
    // create a snapshot of the breakpoints manager
    clDebuggerBreakpoint::Vec_t all_bps;
    clGetManager()->GetAllBreakpoints(all_bps);

    for(const auto& bp : all_bps) {
        if(bp.file.empty() || bp.lineno <= 0)
            continue;

        if(m_ui_breakpoints.count(bp.file) == 0) {
            m_ui_breakpoints.insert({ bp.file, {} });
        }
        m_ui_breakpoints[bp.file].push_back(bp);
    }

    clGetManager()->DeleteAllBreakpoints();
    EventNotifier::Get()->Bind(wxEVT_DBG_UI_TOGGLE_BREAKPOINT, &BreakpointsHelper::OnToggleBreakpoint, this);
}

BreakpointsHelper::~BreakpointsHelper()
{
    // restore the breakpoints
    clDebuggerBreakpoint::Vec_t all_bps;
    for(const auto& vt : m_ui_breakpoints) {
        LOG_DEBUG(LOG) << "Restoring breakpoints for file:" << vt.first << " -" << vt.second.size() << "breakpoints"
                       << endl;
        for(const auto& bp : vt.second) {
            all_bps.push_back(bp);
        }
    }

    clGetManager()->SetBreakpoints(all_bps);
    LOG_DEBUG(LOG) << "Restoring breakpoints...done" << endl;

    // refresh markers
    EventNotifier::Get()->Unbind(wxEVT_DBG_UI_TOGGLE_BREAKPOINT, &BreakpointsHelper::OnToggleBreakpoint, this);
}

void BreakpointsHelper::OnToggleBreakpoint(clDebugEvent& event)
{
    // this instance only exists while an active debug session is running
    // during such a session, we capture all the UI breakpoints adding/deleting
    event.Skip();
    LOG_DEBUG(LOG) << "Toggle breakpoint called for:" << event.GetFileName() << ":" << event.GetLineNumber() << endl;
    if(m_ui_breakpoints.count(event.GetFileName()) == 0) {
        m_ui_breakpoints.insert({ event.GetFileName(), {} });
    }

    auto& file_breakpoints = m_ui_breakpoints[event.GetFileName()];
    auto iter = std::find_if(file_breakpoints.begin(), file_breakpoints.end(), [&](const clDebuggerBreakpoint& bp) {
        return bp.file == event.GetFileName() && bp.lineno == event.GetLineNumber();
    });

    if(iter == file_breakpoints.end()) {
        auto bp = clGetManager()->CreateBreakpoint(event.GetFileName(), event.GetLineNumber());
        file_breakpoints.push_back(bp);
        LOG_DEBUG(LOG) << "Breakpoint does not exist - adding it" << endl;
    } else {
        file_breakpoints.erase(iter);
        LOG_DEBUG(LOG) << "Breakpoint exists - deleting it" << endl;
    }

    // need to apply the breakpoints for this file
    ApplyBreakpoints(event.GetFileName());
}

void BreakpointsHelper::ApplyBreakpoints(const wxString& path)
{
    if(!m_client.IsConnected()) {
        return;
    }
    LOG_DEBUG(LOG) << "Applying breakpoints" << endl;
    std::unordered_map<wxString, std::vector<dap::SourceBreakpoint>> dap_source_breakpoints;
    std::vector<dap::FunctionBreakpoint> dap_function_breakpoints;

    if(m_ui_breakpoints.empty()) {
        return;
    }

    dap_source_breakpoints.reserve(m_ui_breakpoints.size());
    dap_function_breakpoints.reserve(m_ui_breakpoints.size());

    for(const auto& vt : m_ui_breakpoints) {
        if(vt.second.empty()) {
            // no breakpoints for this source file, we need to pass an empty array to dap
            dap_source_breakpoints.insert({ vt.first, {} });

        } else {
            for(const auto& bp : vt.second) {
                if(!is_source_breakpoint(bp)) {
                    continue;
                }
                if(path.empty() || path == bp.file) {
                    // all breakpoints
                    if(dap_source_breakpoints.count(bp.file) == 0) {
                        dap_source_breakpoints.insert({ bp.file, {} });
                    }
                    dap_source_breakpoints[bp.file].push_back(to_dap_source_bp(bp));
                }
            }
        }
    }

    // dont pass empty array, it will tell dap to clear all breakpoints
    for(const auto& vt : dap_source_breakpoints) {
        wxFileName filepath(vt.first);
        LOG_DEBUG(LOG) << "Applying breakpoints for file:" << filepath << endl;
        for(const auto& bp : vt.second) {
            LOG_DEBUG(LOG) << "Line:" << bp.line << ". Condition: " << bp.condition << endl;
        }
        wxString source_path = NormalisePathForSend(filepath.GetFullPath());
        m_client.SetBreakpointsFile(source_path, vt.second);
    }

    if(!dap_function_breakpoints.empty()) {
        LOG_DEBUG(LOG) << "Applying function breakpoints:" << endl;
        for(const auto& bp : dap_function_breakpoints) {
            LOG_DEBUG(LOG) << "Function name:" << bp.name << ". Condition: " << bp.condition << endl;
        }
        m_client.SetFunctionBreakpoints(dap_function_breakpoints);
    }
}

wxString BreakpointsHelper::NormalisePathForSend(const wxString& path) const
{
    wxFileName fn(path);

    // easy path
    if(m_session.dap_server.UseRelativePath()) {
        return fn.GetFullName();
    }

    const auto& dap = m_session.dap_server;

    // attempt to make it fullpath
    if(fn.IsRelative() && !dap.UseRelativePath()) {
        fn.MakeAbsolute(m_session.working_directory);
    }

    if(!dap.UseVolume()) {
        // no volume
        fn.SetVolume(wxEmptyString);
    }

    wxString fullpath = fn.GetFullPath();
    if(dap.UseForwardSlash()) {
        fullpath.Replace(R"(\)", "/");
    }
    return fullpath;
}