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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 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 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
/*
* GDB-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 "gdbvariable.h"
#include "gdbcommand.h"
#include "debugsession.h"
#include <interfaces/icore.h>
#include <debugger/interfaces/ivariablecontroller.h>
using namespace KDevelop;
using namespace GDBDebugger;
QMap<QString, GdbVariable*> GdbVariable::allVariables_;
static bool hasStartedSession()
{
if (!ICore::self()->debugController()) return false; //happens on shutdown
IDebugSession *session = ICore::self()->debugController()->currentSession();
if (!session)
return false;
IDebugSession::DebuggerState s = session->state();
return s != IDebugSession::NotStartedState
&& s != IDebugSession::EndedState;
}
GdbVariable::GdbVariable(TreeModel* model, TreeItem* parent,
const QString& expression, const QString& display)
: Variable(model, parent, expression, display)
{
}
GdbVariable::~GdbVariable()
{
if (!varobj_.isEmpty())
{
// Delete only top-level variable objects.
if (topLevel()) {
if (hasStartedSession()) {
// FIXME: Eventually, should be a property of variable.
IDebugSession* is = ICore::self()->debugController()->currentSession();
DebugSession* s = static_cast<DebugSession*>(is);
s->addCommand(new GDBCommand(GDBMI::VarDelete,
QString("\"%1\"").arg(varobj_)));
}
}
allVariables_.remove(varobj_);
}
}
GdbVariable* GdbVariable::findByVarobjName(const QString& varobjName)
{
if (allVariables_.count(varobjName) == 0)
return 0;
return allVariables_[varobjName];
}
void GdbVariable::setVarobj(const QString& v)
{
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
allVariables_.remove(varobj_);
}
varobj_ = v;
allVariables_[varobj_] = this;
}
static int nextId = 0;
class CreateVarobjHandler : public GDBCommandHandler
{
public:
CreateVarobjHandler(GdbVariable *variable, QObject *callback, const char *callbackMethod)
: m_variable(variable), m_callback(callback), m_callbackMethod(callbackMethod)
{}
virtual void handle(const GDBMI::ResultRecord &r)
{
if (!m_variable) return;
bool hasValue = false;
if (r.reason == "error") {
/* Probably should mark this disabled, or something. */
} else {
m_variable->deleteChildren();
m_variable->setInScope(true);
m_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;
m_variable->setHasMore(hasMore);
m_variable->setValue(r["value"].literal());
hasValue = !r["value"].literal().isEmpty();
if (m_variable->isExpanded() && r["numchild"].toInt()) {
m_variable->fetchMoreChildren();
}
}
if (m_callback && m_callbackMethod) {
QMetaObject::invokeMethod(m_callback, m_callbackMethod, Q_ARG(bool, hasValue));
}
}
virtual bool handlesError() { return true; }
private:
QPointer<GdbVariable> m_variable;
QObject *m_callback;
const char *m_callbackMethod;
};
void GdbVariable::attachMaybe(QObject *callback, const char *callbackMethod)
{
if (!varobj_.isEmpty())
return;
if (hasStartedSession()) {
// FIXME: Eventually, should be a property of variable.
IDebugSession* is = ICore::self()->debugController()->currentSession();
DebugSession* s = static_cast<DebugSession*>(is);
s->addCommand(
new GDBCommand(
GDBMI::VarCreate,
QString("var%1 @ %2").arg(nextId++).arg(enquotedExpression()),
new CreateVarobjHandler(this, callback, callbackMethod)));
}
}
void GdbVariable::markAllDead()
{
QMap<QString, GdbVariable*>::iterator i, e;
for (i = allVariables_.begin(), e = allVariables_.end(); i != e; ++i)
{
i.value()->varobj_.clear();
}
allVariables_.clear();
}
class FetchMoreChildrenHandler : public GDBCommandHandler
{
public:
FetchMoreChildrenHandler(GdbVariable *variable, DebugSession *session)
: m_variable(variable), m_session(session), m_activeCommands(1)
{}
virtual void handle(const GDBMI::ResultRecord &r)
{
if (!m_variable) return;
--m_activeCommands;
if (r.hasField("children"))
{
const GDBMI::Value& children = r["children"];
for (int i = 0; i < children.size(); ++i) {
const GDBMI::Value& child = children[i];
const QString& exp = child["exp"].literal();
if (exp == "public" || exp == "protected" || exp == "private") {
++m_activeCommands;
m_session->addCommand(
new GDBCommand(GDBMI::VarListChildren,
QString("--all-values \"%1\"")
.arg(child["name"].literal()),
this/*use again as handler*/));
} else {
KDevelop::Variable* xvar = m_session->variableController()->
createVariable(m_variable->model(), m_variable,
child["exp"].literal());
GdbVariable* var = static_cast<GdbVariable*>(xvar);
var->setTopLevel(false);
var->setVarobj(child["name"].literal());
var->setHasMoreInitial(child["numchild"].toInt());
m_variable->appendChild(var);
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();
m_variable->setHasMore(hasMore);
if (m_activeCommands == 0) {
delete this;
}
}
virtual bool handlesError() {
// FIXME: handle error?
return false;
}
virtual bool autoDelete() {
// we delete ourselve
return false;
}
private:
QPointer<GdbVariable> m_variable;
DebugSession *m_session;
int m_activeCommands;
};
void GdbVariable::fetchMoreChildren()
{
int c = childItems.size();
// 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.
IDebugSession* is = ICore::self()->debugController()->currentSession();
DebugSession* s = static_cast<DebugSession*>(is);
s->addCommand(
new GDBCommand(GDBMI::VarListChildren,
QString("--all-values \"%1\" %2 %3").arg(varobj_)
.arg(c).arg(c+5),
new FetchMoreChildrenHandler(this, s)));
}
}
void GdbVariable::handleUpdate(const GDBMI::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 GDBMI::Value& children = var["new_children"];
for (int i = 0; i < children.size(); ++i) {
const GDBMI::Value& child = children[i];
const QString& exp = child["exp"].literal();
IDebugSession* is = ICore::self()->debugController()->currentSession();
DebugSession* s = static_cast<DebugSession*>(is);
KDevelop::Variable* xvar = s->variableController()->
createVariable(model(), this, exp);
GdbVariable* var = static_cast<GdbVariable*>(xvar);
var->setTopLevel(false);
var->setVarobj(child["name"].literal());
var->setHasMoreInitial(child["numchild"].toInt() != 0);
appendChild(var);
var->setValue(child["value"].literal());
}
}
setValue(var["value"].literal());
setHasMore(var.hasField("has_more") && var["has_more"].toInt());
}
}
const QString& GdbVariable::varobj() const
{
return varobj_;
}
QString GdbVariable::enquotedExpression() const
{
QString expr = expression();
expr.replace('"', "\\\"");
expr = expr.prepend('"').append('"');
return expr;
}
|