mivariable.cpp 11.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * MI based debugger specific Variable
 *
 * Copyright 2009 Vladimir Prus <ghost@cs.msu.su>
 *
 * 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 "mivariable.h"

24
#include "debuglog.h"
25
#include "midebugsession.h"
26
27
28
29
30
31
#include "mi/micommand.h"

#include <debugger/interfaces/ivariablecontroller.h>
#include <interfaces/icore.h>

using namespace KDevelop;
32
33
using namespace KDevMI;
using namespace KDevMI::MI;
34

35
bool MIVariable::sessionIsAlive() const
36
{
37
    if (!debugSession)
38
39
        return false;

40
    IDebugSession::DebuggerState s = debugSession->state();
41
42
43
44
    return s != IDebugSession::NotStartedState 
        && s != IDebugSession::EndedState;
}

45
MIVariable::MIVariable(MIDebugSession *session, TreeModel* model, TreeItem* parent,
46
47
                       const QString& expression, const QString& display)
    : Variable(model, parent, expression, display)
48
    , debugSession(session)
49
50
51
52
53
54
55
56
57
{
}

MIVariable::~MIVariable()
{
    if (!varobj_.isEmpty())
    {
        // Delete only top-level variable objects.
        if (topLevel()) {
58
59
            if (sessionIsAlive()) {
                debugSession->addCommand(VarDelete, QString("\"%1\"").arg(varobj_));
60
61
            }
        }
62
63
        if (debugSession)
            debugSession->variableMapping().remove(varobj_);
64
65
66
67
68
    }
}

void MIVariable::setVarobj(const QString& v)
{
69
    if (!debugSession) {
Peifeng Yu's avatar
Peifeng Yu committed
70
        qCWarning(DEBUGGERCOMMON) << "MIVariable::setVarobj called when its session died";
71
72
        return;
    }
73
74
75
76
    if (!varobj_.isEmpty()) {
        // this should not happen
        // but apperently it does when attachMaybe is called a second time before
        // the first -var-create call returned
77
        debugSession->variableMapping().remove(varobj_);
78
79
    }
    varobj_ = v;
80
    debugSession->variableMapping()[varobj_] = this;
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
}


static int nextId = 0;

class CreateVarobjHandler : public MICommandHandler
{
public:
    CreateVarobjHandler(MIVariable *variable, QObject *callback, const char *callbackMethod)
    : m_variable(variable), m_callback(callback), m_callbackMethod(callbackMethod)
    {}

    void handle(const ResultRecord &r) override
    {
        if (!m_variable) return;
        bool hasValue = false;
        MIVariable* variable = m_variable.data();
        variable->deleteChildren();
        variable->setInScope(true);
        if (r.reason == "error") {
            variable->setShowError(true);
        } else {
            variable->setVarobj(r["name"].literal());

            bool hasMore = false;
            if (r.hasField("has_more") && r["has_more"].toInt())
                // GDB swears there are more children. Trust it
                hasMore = true;
            else
                // There are no more children in addition to what
                // numchild reports. But, in KDevelop, the variable
                // is not yet expanded, and those numchild are not
                // fetched yet. So, if numchild != 0, hasMore should
                // be true.
                hasMore = r["numchild"].toInt() != 0;

            variable->setHasMore(hasMore);

            variable->setType(r["type"].literal());
            variable->setValue(r["value"].literal());
            hasValue = !r["value"].literal().isEmpty();
            if (variable->isExpanded() && r["numchild"].toInt()) {
                variable->fetchMoreChildren();
            }

            if (variable->format() != KDevelop::Variable::Natural) {
                //TODO doesn't work for children as they are not yet loaded
                variable->formatChanged();
            }
        }

        if (m_callback && m_callbackMethod) {
            QMetaObject::invokeMethod(m_callback, m_callbackMethod, Q_ARG(bool, hasValue));
        }
    }
    bool handlesError() override { return true; }

private:
    QPointer<MIVariable> m_variable;
    QObject *m_callback;
    const char *m_callbackMethod;
};

void MIVariable::attachMaybe(QObject *callback, const char *callbackMethod)
{
    if (!varobj_.isEmpty())
        return;

149
150
151
152
153
154
155
156
    // Try find a current session and attach to it
    if (!ICore::self()->debugController()) return; //happens on shutdown
    debugSession = static_cast<MIDebugSession*>(ICore::self()->debugController()->currentSession());

    if (sessionIsAlive()) {
        debugSession->addCommand(VarCreate,
                                 QString("var%1 @ %2").arg(nextId++).arg(enquotedExpression()),
                                 new CreateVarobjHandler(this, callback, callbackMethod));
157
158
159
    }
}

160
void MIVariable::markAsDead()
161
{
162
    varobj_.clear();
163
164
165
166
167
}

class FetchMoreChildrenHandler : public MICommandHandler
{
public:
168
    FetchMoreChildrenHandler(MIVariable *variable, MIDebugSession *session)
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
        : m_variable(variable), m_session(session), m_activeCommands(1)
    {}

    void handle(const ResultRecord &r) override
    {
        if (!m_variable) return;
        --m_activeCommands;

        MIVariable* variable = m_variable.data();

        if (r.hasField("children"))
        {
            const Value& children = r["children"];
            for (int i = 0; i < children.size(); ++i) {
                const Value& child = children[i];
                const QString& exp = child["exp"].literal();
                if (exp == "public" || exp == "protected" || exp == "private") {
                    ++m_activeCommands;
187
188
189
                    m_session->addCommand(VarListChildren,
                                          QString("--all-values \"%1\"").arg(child["name"].literal()),
                                          this/*use again as handler*/);
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
220
221
222
223
224
225
226
227
228
229
230
                } else {
                    KDevelop::Variable* xvar = m_session->variableController()->
                        createVariable(variable->model(), variable,
                                       child["exp"].literal());
                    MIVariable* var = static_cast<MIVariable*>(xvar);
                    var->setTopLevel(false);
                    var->setVarobj(child["name"].literal());
                    bool hasMore = child["numchild"].toInt() != 0 || ( child.hasField("dynamic") && child["dynamic"].toInt()!=0 );
                    var->setHasMoreInitial(hasMore);
                    variable->appendChild(var);
                    var->setType(child["type"].literal());
                    var->setValue(child["value"].literal());
                }
            }
        }

        /* Note that we don't set hasMore to true if there are still active
           commands. The reason is that we don't want the user to have
           even theoretical ability to click on "..." item and confuse
           us.  */
        bool hasMore = false;
        if (r.hasField("has_more"))
            hasMore = r["has_more"].toInt();

        variable->setHasMore(hasMore);
        if (m_activeCommands == 0) {
            variable->emitAllChildrenFetched();
            delete this;
        }
    }
    bool handlesError() override {
        // FIXME: handle error?
        return false;
    }
    bool autoDelete() override {
        // we delete ourselve
        return false;
    }

private:
    QPointer<MIVariable> m_variable;
231
    MIDebugSession *m_session;
232
233
234
235
236
237
238
239
    int m_activeCommands;
};

void MIVariable::fetchMoreChildren()
{
    int c = childItems.size();
    // FIXME: should not even try this if app is not started.
    // Probably need to disable open, or something
240
241
242
243
244
245
    if (sessionIsAlive()) {
        debugSession->addCommand(VarListChildren,
                                 QString("--all-values \"%1\" %2 %3")
                                 //   fetch    from ..    to ..
                                 .arg(varobj_).arg(c).arg(c + fetchStep),
                                 new FetchMoreChildrenHandler(this, debugSession));
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
    }
}

void MIVariable::handleUpdate(const Value& var)
{
    if (var.hasField("type_changed")
        && var["type_changed"].literal() == "true")
    {
        deleteChildren();
        // FIXME: verify that this check is right.
        setHasMore(var["new_num_children"].toInt() != 0);
        fetchMoreChildren();
    }

    if (var.hasField("in_scope") && var["in_scope"].literal() == "false")
    {
        setInScope(false);
    }
    else
    {
        setInScope(true);

        if  (var.hasField("new_num_children")) {
            int nc = var["new_num_children"].toInt();
            Q_ASSERT(nc != -1);
            setHasMore(false);
            while (childCount() > nc) {
                TreeItem *c = child(childCount()-1);
                removeChild(childCount()-1);
                delete c;
            }
        }

        // FIXME: the below code is essentially copy-paste from
        // FetchMoreChildrenHandler. We need to introduce GDB-specific
        // subclass of KDevelop::Variable that is capable of creating
        // itself from MI output directly, and relay to that.
        if (var.hasField("new_children"))
        {
            const Value& children = var["new_children"];
            for (int i = 0; i < children.size(); ++i) {
                const Value& child = children[i];
                const QString& exp = child["exp"].literal();

290
291
292
293
294
295
296
297
298
299
300
301
                if (debugSession) {
                    auto xvar = debugSession->variableController()->createVariable(model(), this, exp);
                    auto var = static_cast<MIVariable*>(xvar);
                    var->setTopLevel(false);
                    var->setVarobj(child["name"].literal());
                    bool hasMore = child["numchild"].toInt() != 0 || ( child.hasField("dynamic") && child["dynamic"].toInt()!=0 );
                    var->setHasMoreInitial(hasMore);
                    appendChild(var);
                    var->setType(child["type"].literal());
                    var->setValue(child["value"].literal());
                    var->setChanged(true);
                }
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
            }
        }

        if (var.hasField("type_changed") && var["type_changed"].literal() == "true") {
            setType(var["new_type"].literal());
        }
        setValue(var["value"].literal());
        setChanged(true);
        setHasMore(var.hasField("has_more") && var["has_more"].toInt());
    }
}

const QString& MIVariable::varobj() const
{
    return varobj_;
}

QString MIVariable::enquotedExpression() const
{
    QString expr = expression();
    expr.replace('"', "\\\"");
    expr = expr.prepend('"').append('"');
    return expr;
}


class SetFormatHandler : public MICommandHandler
{
public:
    SetFormatHandler(MIVariable *var)
        : m_variable(var)
    {}

    void handle(const ResultRecord &r) override
    {
        if(r.hasField("value"))
            m_variable.data()->setValue(r["value"].literal());
    }
private:
    QPointer<MIVariable> m_variable;
};

void MIVariable::formatChanged()
{
    if(childCount())
    {
        foreach(TreeItem* item, childItems) {
            Q_ASSERT(dynamic_cast<MIVariable*>(item));
            if( MIVariable* var=dynamic_cast<MIVariable*>(item))
                var->setFormat(format());
        }
    }
    else
    {
356
357
358
359
        if (sessionIsAlive()) {
            debugSession->addCommand(VarSetFormat,
                                     QString(" \"%1\" %2 ").arg(varobj_).arg(format2str(format())),
                                     new SetFormatHandler(this));
360
361
362
        }
    }
}