debuggerrunner.cpp 13 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>
hjk's avatar
hjk committed
59

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

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

67

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

74 75
namespace Debugger {

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

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

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

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

101 102 103 104 105 106 107 108 109 110
    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();
111

112 113
    // Find qtInstallPath.
    QString qmakePath = DebuggingHelperLibrary::findSystemQt(rc->environment());
114 115 116 117 118 119 120 121 122
    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) + "/..");
123
        sp.qtInstallPath = fi.absoluteFilePath();
124 125
    }
    return sp;
126 127
}

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

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

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


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

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

public:
    DebuggerRunControl *q;

    DebuggerStartParameters m_startParameters;
    DebuggerManager *m_manager;
167
    Internal::IDebuggerEngine *m_engine;
168 169
    bool m_running;

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

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

DebuggerRunControl::Private::~Private()
{
#define doDelete(ptr) delete ptr; ptr = 0
    doDelete(m_modulesHandler);
200
    doDelete(m_registerHandler);
201
    doDelete(m_snapshotHandler);
202
#undef doDelete
203 204
}

con's avatar
con committed
205 206 207 208 209 210 211 212

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

DebuggerRunControl::DebuggerRunControl(DebuggerManager *manager,
213
        const DebuggerStartParameters &startParameters)
214
    : RunControl(0, ProjectExplorer::Constants::DEBUGMODE),
215
      d(new Private(this, manager, startParameters))
216
{
217
    connect(d->m_manager, SIGNAL(debuggingFinished()),
218 219
            this, SLOT(debuggingFinished()),
            Qt::QueuedConnection);
220
    connect(d->m_manager, SIGNAL(messageAvailable(QString, bool)),
221
            this, SLOT(slotMessageAvailable(QString, bool)));
222
    connect(d->m_manager, SIGNAL(inferiorPidChanged(qint64)),
223 224 225
            this, SLOT(bringApplicationToForeground(qint64)),
            Qt::QueuedConnection);
    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 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

//////////////////////////////////////////////////////////////////////
//
// 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
470
} // namespace Debugger