File: ibreakpointcontroller.cpp

package info (click to toggle)
kdevelop 4%3A22.12.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 70,096 kB
  • sloc: cpp: 284,635; javascript: 3,558; python: 3,422; sh: 1,319; ansic: 685; xml: 331; php: 95; lisp: 66; makefile: 39; sed: 12
file content (219 lines) | stat: -rw-r--r-- 7,097 bytes parent folder | download
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
    SPDX-FileCopyrightText: 2002 Matthias Hoelzer-Kluepfel <hoelzer@kde.org>
    SPDX-FileCopyrightText: 2002 John Firebaugh <jfirebaugh@kde.org>
    SPDX-FileCopyrightText: 2006, 2008 Vladimir Prus <ghost@cs.msu.su>
    SPDX-FileCopyrightText: 2007 Hamish Rodda <rodda@kde.org>
    SPDX-FileCopyrightText: 2009 Niko Sams <niko.sams@gmail.com>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/


#include "ibreakpointcontroller.h"

#include <KNotification>
#include <KLocalizedString>
#include <KParts/MainWindow>

#include "idebugsession.h"
#include "../../interfaces/icore.h"
#include "../breakpoint/breakpointmodel.h"
#include "../../interfaces/idebugcontroller.h"
#include "../breakpoint/breakpoint.h"
#include "../../interfaces/iuicontroller.h"
#include <debug.h>

namespace KDevelop {

IBreakpointController::IBreakpointController(KDevelop::IDebugSession* parent)
    : QObject(parent), m_dontSendChanges(0)
{
    connect(parent, &IDebugSession::stateChanged,
             this, &IBreakpointController::debuggerStateChanged);
}

IDebugSession* IBreakpointController::debugSession() const
{
    return static_cast<IDebugSession*>(const_cast<QObject*>(QObject::parent()));
}

BreakpointModel* IBreakpointController::breakpointModel() const
{
    if (!ICore::self()) return nullptr;
    return ICore::self()->debugController()->breakpointModel();
}

void IBreakpointController::updateState(int row, Breakpoint::BreakpointState state)
{
    breakpointModel()->updateState(row, state);
}

void IBreakpointController::updateHitCount(int row, int hitCount)
{
    breakpointModel()->updateHitCount(row, hitCount);
}

void IBreakpointController::updateErrorText(int row, const QString& errorText)
{
    breakpointModel()->updateErrorText(row, errorText);
}

void IBreakpointController::notifyHit(int row, const QString& msg)
{
    BreakpointModel* model = breakpointModel();
    model->notifyHit(row);

    // This is a slightly odd place to issue this notification,
    // but then again it's not clear which place would be more natural
    Breakpoint* breakpoint = model->breakpoint(row);
    KNotification* ev = nullptr;
    switch(breakpoint->kind()) {
        case Breakpoint::CodeBreakpoint:
            ev = new KNotification(QStringLiteral("BreakpointHit"));
            ev->setWidget(ICore::self()->uiController()->activeMainWindow());
            ev->setText(i18n("Breakpoint hit: %1", breakpoint->location()) + msg);
            break;
        case Breakpoint::WriteBreakpoint:
        case Breakpoint::ReadBreakpoint:
        case Breakpoint::AccessBreakpoint:
            ev = new KNotification(QStringLiteral("WatchpointHit"));
            ev->setWidget(ICore::self()->uiController()->activeMainWindow());
            ev->setText(i18n("Watchpoint hit: %1", breakpoint->location()) + msg);
            break;
        default:
            Q_ASSERT(0);
            break;
    }
    if (ev) {
        ev->setPixmap(QIcon::fromTheme(QStringLiteral("breakpoint")).pixmap(QSize(22,22)));
        // TODO: Port
        //ev->setComponentName(ICore::self()->aboutData().componentName());
        ev->sendEvent();
    }
}

// Temporary: empty default implementation
void IBreakpointController::breakpointAdded(int row)
{
    Q_UNUSED(row);
}

// Temporary: implement old-style behavior to ease transition through API changes
void IBreakpointController::breakpointModelChanged(int row, BreakpointModel::ColumnFlags columns)
{
    if (m_dontSendChanges)
        return;

    if ((columns & ~BreakpointModel::StateColumnFlag) != 0) {
        Breakpoint * breakpoint = breakpointModel()->breakpoint(row);
        for (int column = 0; column < BreakpointModel::NumColumns; ++column) {
            if (columns & (1 << column)) {
                m_dirty[breakpoint].insert(Breakpoint::Column(column));
                auto errorIt = m_errors.find(breakpoint);
                if (errorIt != m_errors.end()) {
                    errorIt->remove(Breakpoint::Column(column));
                }
            }
        }
        breakpointStateChanged(breakpoint);
        if (debugSession()->isRunning()) {
            sendMaybe(breakpoint);
        }
    }
}

void IBreakpointController::debuggerStateChanged(IDebugSession::DebuggerState state)
{
    BreakpointModel* model = breakpointModel();
    if (!model)
        return;

    //breakpoint state changes when session started or stopped
    const auto breakpoints = model->breakpoints();
    for (Breakpoint* breakpoint : breakpoints) {
        if (state == IDebugSession::StartingState) {
            auto& dirty = m_dirty[breakpoint];

            //when starting everything is dirty
            dirty.insert(Breakpoint::LocationColumn);
            if (!breakpoint->condition().isEmpty()) {
                dirty.insert(Breakpoint::ConditionColumn);
            }
            if (!breakpoint->enabled()) {
                dirty.insert(KDevelop::Breakpoint::EnableColumn);
            }
        }
        breakpointStateChanged(breakpoint);
    }
}

void IBreakpointController::sendMaybeAll()
{
    BreakpointModel* model = breakpointModel();
    if (!model)
        return;

    const auto breakpoints = model->breakpoints();
    for (Breakpoint* breakpoint : breakpoints) {
        sendMaybe(breakpoint);
    }
}

// Temporary implementation to ease the API transition
void IBreakpointController::breakpointAboutToBeDeleted(int row)
{
    Breakpoint* breakpoint = breakpointModel()->breakpoint(row);
    qCDebug(DEBUGGER) << "breakpointAboutToBeDeleted(" << row << "): " << breakpoint;
    sendMaybe(breakpoint);
}

void IBreakpointController::breakpointStateChanged(Breakpoint* breakpoint)
{
    if (breakpoint->deleted()) return;

    Breakpoint::BreakpointState newState = Breakpoint::NotStartedState;
    if (debugSession()->state() != IDebugSession::EndedState &&
        debugSession()->state() != IDebugSession::NotStartedState)
    {
        if (m_dirty.value(breakpoint).isEmpty()) {
            if (m_pending.contains(breakpoint)) {
                newState = Breakpoint::PendingState;
            } else {
                newState = Breakpoint::CleanState;
            }
        } else {
            newState = Breakpoint::DirtyState;
        }
    }

    m_dontSendChanges++;
    updateState(breakpointModel()->breakpointIndex(breakpoint, 0).row(), newState);
    m_dontSendChanges--;
}

void IBreakpointController::setHitCount(Breakpoint* breakpoint, int count)
{
    m_dontSendChanges++;
    updateHitCount(breakpointModel()->breakpointIndex(breakpoint, 0).row(), count);
    m_dontSendChanges--;
}

void IBreakpointController::error(Breakpoint* breakpoint, const QString &msg, Breakpoint::Column column)
{
    BreakpointModel* model = breakpointModel();
    int row = model->breakpointIndex(breakpoint, 0).row();

    m_dontSendChanges++;
    m_errors[breakpoint].insert(column);
    updateErrorText(row, msg);
    m_dontSendChanges--;
}

void IBreakpointController::hit(KDevelop::Breakpoint* breakpoint, const QString &msg)
{
    int row = breakpointModel()->breakpointIndex(breakpoint, 0).row();
    notifyHit(row, msg);
}

}