debuggerrunner.cpp 13.4 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2 3 4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11 12 13 14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18 19 20 21 22 23
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
#include "debuggerrunner.h"
hjk's avatar
hjk committed
31
#include "debuggermanager.h"
32
#include "debuggeroutputwindow.h"
con's avatar
con committed
33

34
#include "idebuggerengine.h"
35 36

#include "breakhandler.h"
37
#include "moduleshandler.h"
38 39 40 41 42 43
#include "registerhandler.h"
#include "snapshothandler.h"
#include "stackhandler.h"
#include "stackframe.h"
#include "threadshandler.h"
#include "watchhandler.h"
44

hjk's avatar
hjk committed
45
#include <projectexplorer/debugginghelper.h>
con's avatar
con committed
46 47 48
#include <projectexplorer/environment.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectexplorerconstants.h>
Tobias Hunger's avatar
Tobias Hunger committed
49
#include <projectexplorer/target.h>
50
#include <projectexplorer/buildconfiguration.h>
51
#include <projectexplorer/applicationrunconfiguration.h> // For LocalApplication*
con's avatar
con committed
52

hjk's avatar
hjk committed
53
#include <utils/qtcassert.h>
54
#include <coreplugin/icore.h>
hjk's avatar
hjk committed
55

con's avatar
con committed
56 57 58
#include <QtCore/QDebug>
#include <QtCore/QDir>
#include <QtCore/QFileInfo>
59
#include <QtCore/QTimer>
hjk's avatar
hjk committed
60

61
#include <QtGui/QAbstractItemView>
62
#include <QtGui/QTextDocument>
63
#include <QtGui/QTreeWidget>
con's avatar
con committed
64

65
using namespace ProjectExplorer;
66 67
using namespace Debugger::Internal;

68

con's avatar
con committed
69 70
////////////////////////////////////////////////////////////////////////
//
71
// DebuggerRunControlFactory
con's avatar
con committed
72 73 74
//
////////////////////////////////////////////////////////////////////////

75 76
namespace Debugger {

77
// A factory to create DebuggerRunControls
78
DebuggerRunControlFactory::DebuggerRunControlFactory(DebuggerManager *manager)
79
    : m_manager(manager)
con's avatar
con committed
80 81
{}

82
bool DebuggerRunControlFactory::canRun(RunConfiguration *runConfiguration, const QString &mode) const
con's avatar
con committed
83
{
84
//    return mode == ProjectExplorer::Constants::DEBUGMODE;
85
    return mode == ProjectExplorer::Constants::DEBUGMODE
86
            && qobject_cast<LocalApplicationRunConfiguration *>(runConfiguration);
con's avatar
con committed
87 88
}

89
QString DebuggerRunControlFactory::displayName() const
con's avatar
con committed
90
{
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
91
    return tr("Debug");
con's avatar
con committed
92 93
}

94
static DebuggerStartParameters localStartParameters(RunConfiguration *runConfiguration)
95
{
96
    DebuggerStartParameters sp;
97 98 99 100 101
    QTC_ASSERT(runConfiguration, return sp);
    LocalApplicationRunConfiguration *rc =
            qobject_cast<LocalApplicationRunConfiguration *>(runConfiguration);
    QTC_ASSERT(rc, return sp);

102 103 104 105 106 107 108 109 110 111
    sp.startMode = StartInternal;
    sp.executable = rc->executable();
    sp.environment = rc->environment().toStringList();
    sp.workingDirectory = rc->workingDirectory();
    sp.processArgs = rc->commandLineArguments();
    sp.toolChainType = rc->toolChainType();
    sp.useTerminal = rc->runMode() == LocalApplicationRunConfiguration::Console;
    sp.dumperLibrary = rc->dumperLibrary();
    sp.dumperLibraryLocations = rc->dumperLibraryLocations();
    sp.displayName = rc->displayName();
112

113 114
    // Find qtInstallPath.
    QString qmakePath = DebuggingHelperLibrary::findSystemQt(rc->environment());
115 116 117 118 119 120 121 122 123
    if (!qmakePath.isEmpty()) {
        QProcess proc;
        QStringList args;
        args.append(QLatin1String("-query"));
        args.append(QLatin1String("QT_INSTALL_HEADERS"));
        proc.start(qmakePath, args);
        proc.waitForFinished();
        QByteArray ba = proc.readAllStandardOutput().trimmed();
        QFileInfo fi(QString::fromLocal8Bit(ba) + "/..");
124
        sp.qtInstallPath = fi.absoluteFilePath();
125 126
    }
    return sp;
127 128
}

129 130
RunControl *DebuggerRunControlFactory::create
    (RunConfiguration *runConfiguration, const QString &mode)
con's avatar
con committed
131
{
hjk's avatar
hjk committed
132
    QTC_ASSERT(mode == ProjectExplorer::Constants::DEBUGMODE, return 0);
133
    DebuggerStartParameters sp = localStartParameters(runConfiguration);
134
    return new DebuggerRunControl(m_manager, sp);
135 136
}

137
RunControl *DebuggerRunControlFactory::create(const DebuggerStartParameters &sp)
138
{
139
    return new DebuggerRunControl(m_manager, sp);
con's avatar
con committed
140 141
}

142
QWidget *DebuggerRunControlFactory::createConfigurationWidget(RunConfiguration *runConfiguration)
con's avatar
con committed
143 144
{
    // NBS TODO: Add GDB-specific configuration widget
145
    Q_UNUSED(runConfiguration)
con's avatar
con committed
146 147 148 149
    return 0;
}


150 151 152 153 154 155 156 157 158
////////////////////////////////////////////////////////////////////////
//
// DebuggerRunControl::Private
//
////////////////////////////////////////////////////////////////////////

class DebuggerRunControl::Private
{
public:
159 160 161
    Private(DebuggerRunControl *parent, DebuggerManager *manager,
        const DebuggerStartParameters &startParameters);
    ~Private();
162 163 164 165 166 167

public:
    DebuggerRunControl *q;

    DebuggerStartParameters m_startParameters;
    DebuggerManager *m_manager;
168
    Internal::IDebuggerEngine *m_engine;
169
    bool m_running;
170
    qint64 m_inferiorPid;
171

172
    ModulesHandler *m_modulesHandler;
173
    RegisterHandler *m_registerHandler;
174
    SnapshotHandler *m_snapshotHandler;
175 176 177 178 179 180 181 182 183
/*
    // FIXME: Move from DebuggerManager
    BreakHandler *m_breakHandler;
    StackHandler *m_stackHandler;
    ThreadsHandler *m_threadsHandler;
    WatchHandler *m_watchHandler;
*/
};

184 185 186 187 188 189 190
DebuggerRunControl::Private::Private(DebuggerRunControl *parent,
        DebuggerManager *manager,
        const DebuggerStartParameters &startParameters)
  : q(parent),
    m_startParameters(startParameters),
    m_manager(manager),
    m_engine(0)
191
{
192
    m_running = false;
193
    m_inferiorPid = m_startParameters.attachPID > 0 ? m_startParameters.attachPID : 0;
194
    m_modulesHandler = new ModulesHandler(q);
195
    m_registerHandler = new RegisterHandler();
196
    m_snapshotHandler = new SnapshotHandler(q);
197 198 199 200 201 202
}

DebuggerRunControl::Private::~Private()
{
#define doDelete(ptr) delete ptr; ptr = 0
    doDelete(m_modulesHandler);
203
    doDelete(m_registerHandler);
204
    doDelete(m_snapshotHandler);
205
#undef doDelete
206 207
}

con's avatar
con committed
208 209 210 211 212 213 214 215

////////////////////////////////////////////////////////////////////////
//
// DebuggerRunControl
//
////////////////////////////////////////////////////////////////////////

DebuggerRunControl::DebuggerRunControl(DebuggerManager *manager,
216
        const DebuggerStartParameters &startParameters)
217
    : RunControl(0, ProjectExplorer::Constants::DEBUGMODE),
218
      d(new Private(this, manager, startParameters))
219
{
220
    connect(d->m_manager, SIGNAL(debuggingFinished()),
221 222
            this, SLOT(debuggingFinished()),
            Qt::QueuedConnection);
223
    connect(d->m_manager, SIGNAL(messageAvailable(QString, bool)),
224 225
            this, SLOT(slotMessageAvailable(QString, bool)));
    connect(this, SIGNAL(stopRequested()),
226
            d->m_manager, SLOT(exitDebugger()));
227

228 229 230
    if (d->m_startParameters.environment.empty())
        d->m_startParameters.environment = ProjectExplorer::Environment().toStringList();
    d->m_startParameters.useTerminal = false;
231

232 233
}

234 235 236 237 238
DebuggerRunControl::~DebuggerRunControl()
{
    delete d;
}

239 240
QString DebuggerRunControl::displayName() const
{
241
    return d->m_startParameters.displayName;
242 243
}

244 245
void DebuggerRunControl::setCustomEnvironment(ProjectExplorer::Environment env)
{
246
    d->m_startParameters.environment = env.toStringList();
247 248
}

249 250 251 252
void DebuggerRunControl::init()
{
}

253 254
void DebuggerRunControl::start()
{
255
    d->m_running = true;
256 257 258
    QString errorMessage;
    QString settingsCategory;
    QString settingsPage;
259
    if (d->m_manager->checkDebugConfiguration(d->m_startParameters.toolChainType,
260
            &errorMessage, &settingsCategory, &settingsPage)) {
261
        d->m_manager->startNewDebugger(this);
262
        emit started();
263
    } else {
264
        appendMessage(this, errorMessage, true);
265
        emit finished();
266 267
        Core::ICore::instance()->showWarningWithOptions(tr("Debugger"),
            errorMessage, QString(), settingsCategory, settingsPage);
268
    }
con's avatar
con committed
269 270
}

271 272
void DebuggerRunControl::showMessage(const QString &msg, int channel,
    int timeout)
273
{
274
    if (!d->m_manager)
Robert Loehning's avatar
Robert Loehning committed
275
        return;
276
    DebuggerOutputWindow *ow = d->m_manager->debuggerOutputWindow();
277 278 279
    QTC_ASSERT(ow, return);
    switch (channel) {
        case StatusBar:
280
            d->m_manager->showStatusMessage(msg, timeout);
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
            ow->showOutput(LogStatus, msg);
            break;
        case AppOutput:
            emit addToOutputWindowInline(this, msg, false);
            break;
        case AppError:
            emit addToOutputWindowInline(this, msg, true);
            break;
        case LogMiscInput:
            ow->showInput(LogMisc, msg);
            ow->showOutput(LogMisc, msg);
            break;
        case LogInput:
            ow->showInput(channel, msg);
            ow->showOutput(channel, msg);
            break;
        default:
            ow->showOutput(channel, msg);
            break;
    }
301 302 303 304 305
}

void DebuggerRunControl::slotMessageAvailable(const QString &data, bool isError)
{
    emit appendMessage(this, data, isError);
con's avatar
con committed
306 307 308 309
}

void DebuggerRunControl::stop()
{
310
    d->m_running = false;
hjk's avatar
hjk committed
311
    emit stopRequested();
con's avatar
con committed
312 313 314 315
}

void DebuggerRunControl::debuggingFinished()
{
316
    d->m_running = false;
con's avatar
con committed
317 318 319 320 321
    emit finished();
}

bool DebuggerRunControl::isRunning() const
{
322 323 324 325 326 327 328 329 330 331
    return d->m_running;
}

const DebuggerStartParameters &DebuggerRunControl::sp() const
{
    return d->m_startParameters;
}

ModulesHandler *DebuggerRunControl::modulesHandler() const
{
332
    return d->m_modulesHandler;
333 334 335 336 337 338 339 340 341
}

BreakHandler *DebuggerRunControl::breakHandler() const
{
    return d->m_manager->breakHandler();
}

RegisterHandler *DebuggerRunControl::registerHandler() const
{
342
    return d->m_registerHandler;
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
}

StackHandler *DebuggerRunControl::stackHandler() const
{
    return d->m_manager->stackHandler();
}

ThreadsHandler *DebuggerRunControl::threadsHandler() const
{
    return d->m_manager->threadsHandler();
}

WatchHandler *DebuggerRunControl::watchHandler() const
{
    return d->m_manager->watchHandler();
}

SnapshotHandler *DebuggerRunControl::snapshotHandler() const
{
362
    return d->m_snapshotHandler;
con's avatar
con committed
363
}
hjk's avatar
hjk committed
364

365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
void DebuggerRunControl::cleanup()
{
    modulesHandler()->removeAll();
}

Internal::IDebuggerEngine *DebuggerRunControl::engine()
{
    QTC_ASSERT(d->m_engine, /**/);
    return d->m_engine;
}

void DebuggerRunControl::startDebugger(IDebuggerEngine *engine)
{
    d->m_engine = engine;
    d->m_engine->setRunControl(this);
    d->m_manager->modulesWindow()->setModel(d->m_modulesHandler->model());
381
    d->m_manager->registerWindow()->setModel(d->m_registerHandler->model());
382
    d->m_manager->snapshotWindow()->setModel(d->m_snapshotHandler->model());
383 384 385
    d->m_engine->startDebugger();
}

386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
void DebuggerRunControl::notifyInferiorPid(qint64 pid)
{
    //STATE_DEBUG(d->m_inferiorPid << pid);
    if (d->m_inferiorPid == pid)
        return;
    d->m_inferiorPid = pid;
    QTimer::singleShot(0, this, SLOT(raiseApplication()));
}

qint64 DebuggerRunControl::inferiorPid() const
{
    return d->m_inferiorPid;
}

void DebuggerRunControl::raiseApplication()
{
    bringApplicationToForeground(d->m_inferiorPid);
}
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487

//////////////////////////////////////////////////////////////////////
//
// AbstractDebuggerEngine
//
//////////////////////////////////////////////////////////////////////

/*
void IDebuggerEngine::showStatusMessage(const QString &msg, int timeout)
{
    m_manager->showStatusMessage(msg, timeout);
}
*/

DebuggerState IDebuggerEngine::state() const
{
    return m_manager->state();
}

void IDebuggerEngine::setState(DebuggerState state, bool forced)
{
    m_manager->setState(state, forced);
}

bool IDebuggerEngine::debuggerActionsEnabled() const
{
    return m_manager->debuggerActionsEnabled();
}

void IDebuggerEngine::showModuleSymbols
    (const QString &moduleName, const Symbols &symbols)
{
    QTreeWidget *w = new QTreeWidget;
    w->setColumnCount(3);
    w->setRootIsDecorated(false);
    w->setAlternatingRowColors(true);
    w->setSortingEnabled(true);
    w->setHeaderLabels(QStringList() << tr("Symbol") << tr("Address") << tr("Code"));
    w->setWindowTitle(tr("Symbols in \"%1\"").arg(moduleName));
    foreach (const Symbol &s, symbols) {
        QTreeWidgetItem *it = new QTreeWidgetItem;
        it->setData(0, Qt::DisplayRole, s.name);
        it->setData(1, Qt::DisplayRole, s.address);
        it->setData(2, Qt::DisplayRole, s.state);
        w->addTopLevelItem(it);
    }
    manager()->createNewDock(w);
}

ModulesHandler *IDebuggerEngine::modulesHandler() const
{
    return runControl()->modulesHandler();
}

BreakHandler *IDebuggerEngine::breakHandler() const
{
    return runControl()->breakHandler();
}

RegisterHandler *IDebuggerEngine::registerHandler() const
{
    return runControl()->registerHandler();
}

StackHandler *IDebuggerEngine::stackHandler() const
{
    return runControl()->stackHandler();
}

ThreadsHandler *IDebuggerEngine::threadsHandler() const
{
    return runControl()->threadsHandler();
}

WatchHandler *IDebuggerEngine::watchHandler() const
{
    return runControl()->watchHandler();
}

SnapshotHandler *IDebuggerEngine::snapshotHandler() const
{
    return runControl()->snapshotHandler();
}

hjk's avatar
hjk committed
488
} // namespace Debugger