/
variable.cpp
203 lines (175 loc) · 6.37 KB
/
variable.cpp
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
/*
* XDebug-specific Variable
*
* Copyright 2009 Vladimir Prus <ghost@cs.msu.su>
* Copyright 2009 Niko Sams <niko.sams@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "variable.h"
#include "debugsession.h"
#include <QDebug>
#include <QDomElement>
#include <QXmlStreamReader>
#include <interfaces/icore.h>
#include <debugger/interfaces/ivariablecontroller.h>
#include <debugger/interfaces/iframestackmodel.h>
#include "connection.h"
#include "debuggerdebug.h"
namespace XDebug {
static bool hasStartedSession()
{
KDevelop::IDebugSession* session = KDevelop::ICore::self()->debugController()->currentSession();
if (!session) {
return false;
}
KDevelop::IDebugSession::DebuggerState s = session->state();
return s != KDevelop::IDebugSession::NotStartedState
&& s != KDevelop::IDebugSession::EndedState;
}
Variable::Variable(KDevelop::TreeModel* model, KDevelop::TreeItem* parent,
const QString& expression, const QString& display)
: KDevelop::Variable(model, parent, expression, display)
{
}
Variable::~Variable()
{
}
class PropertyGetCallback
: public CallbackBase
{
public:
PropertyGetCallback(Variable* variable, QObject* callback, const char* callbackMethod)
: m_variable(variable)
, m_callback(callback)
, m_callbackMethod(callbackMethod)
{}
void execute(const QDomDocument& xml) override
{
qCDebug(KDEV_PHP_DEBUGGER) << xml.toString();
Q_ASSERT(xml.documentElement().attribute("command") == "property_get");
if (!m_variable) {return;}
bool hasValue = false;
QDomElement el = xml.documentElement().firstChildElement();
if (el.nodeName() == "error") {
qCDebug(KDEV_PHP_DEBUGGER) << el.firstChildElement().text();
//hasValue=false
} else {
el = xml.documentElement().firstChildElement("property");
hasValue = !el.isNull();
if (hasValue) {
m_variable->handleProperty(el);
}
}
if (m_callback && m_callbackMethod) {
QMetaObject::invokeMethod(m_callback, m_callbackMethod, Q_ARG(bool, hasValue));
}
}
bool allowError() const override { return true; }
private:
QPointer<Variable> m_variable;
QObject* m_callback;
const char* m_callbackMethod;
};
void Variable::attachMaybe(QObject* callback, const char* callbackMethod)
{
if (hasStartedSession()) {
// FIXME: Eventually, should be a property of variable.
KDevelop::IDebugSession* is = KDevelop::ICore::self()->debugController()->currentSession();
DebugSession* s = static_cast<DebugSession*>(is);
QStringList args;
args << "-n " + expression();
args << QString("-d %0").arg(s->frameStackModel()->currentFrame());
s->connection()->sendCommand("property_get", args, QByteArray(),
new PropertyGetCallback(this, callback, callbackMethod));
}
}
void Variable::fetchMoreChildren()
{
// FIXME: should not even try this if app is not started.
// Probably need to disable open, or something
if (hasStartedSession()) {
// FIXME: Eventually, should be a property of variable.
KDevelop::IDebugSession* is = KDevelop::ICore::self()->debugController()->currentSession();
DebugSession* s = static_cast<DebugSession*>(is);
qCDebug(KDEV_PHP_DEBUGGER) << expression() << m_fullName;
QStringList args;
args << "-n " + m_fullName;
args << QString("-d %0").arg(s->frameStackModel()->currentFrame());
s->connection()->sendCommand("property_get", args, QByteArray(),
new PropertyGetCallback(this, nullptr, nullptr));
}
}
void Variable::handleProperty(const QDomElement& xml)
{
Q_ASSERT(!xml.isNull());
Q_ASSERT(xml.nodeName() == "property");
setInScope(true);
m_fullName = xml.attribute("fullname");
//qCDebug(KDEV_PHP_DEBUGGER) << m_fullName;
if (xml.firstChild().isText()) {
QString v = xml.firstChild().toText().data();
if (xml.attribute("encoding") == "base64") {
//TODO: use Connection::m_codec->toUnicode
v = QString::fromUtf8(QByteArray::fromBase64(xml.text().toUtf8()));
}
//qCDebug(KDEV_PHP_DEBUGGER) << "value" << v;
setValue(v);
}
QMap<QString, Variable*> existing;
for (int i = 0; i < childCount() - (hasMore() ? 1 : 0); i++) {
Q_ASSERT(dynamic_cast<Variable*>(child(i)));
Variable* v = static_cast<Variable*>(child(i));
existing[v->expression()] = v;
}
QSet<QString> current;
QDomElement el = xml.firstChildElement("property");
while (!el.isNull()) {
QString name = el.attribute("name");
//qCDebug(KDEV_PHP_DEBUGGER) << name;
current << name;
Variable* v = nullptr;
if (!existing.contains(name)) {
v = new Variable(model(), this, name);
appendChild(v, false);
} else {
v = existing[name];
}
v->handleProperty(el);
el = el.nextSiblingElement("property");
}
for (int i = 0; i < childCount() - (hasMore() ? 1 : 0); ++i) {
Q_ASSERT(dynamic_cast<KDevelop::Variable*>(child(i)));
KDevelop::Variable* v = static_cast<KDevelop::Variable*>(child(i));
if (!current.contains(v->expression())) {
removeChild(i);
--i;
delete v;
}
}
if (!childCount() && xml.attribute("children") == "1") {
qCDebug(KDEV_PHP_DEBUGGER) << "has more" << this;
setHasMore(true);
if (isExpanded()) {
fetchMoreChildren();
}
}
}
QString Variable::fullName() const
{
return m_fullName;
}
}