debuggerrunner.cpp 25.8 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 31
#include "debuggerrunner.h"

32
#include "debuggeractions.h"
33
#include "debuggercore.h"
34
#include "debuggerengine.h"
35
#include "debuggermainwindow.h"
36 37
#include "debuggerplugin.h"
#include "debuggerstringutils.h"
38 39 40
#include "gdb/gdbengine.h"
#include "gdb/remotegdbserveradapter.h"
#include "gdb/remoteplaingdbadapter.h"
hjk's avatar
hjk committed
41
#include "gdb/gdboptionspage.h"
42
#include "qml/qmlengine.h"
43
#include "qml/qmlcppengine.h"
44
#include "lldb/lldbenginehost.h"
45

Friedemann Kleint's avatar
Friedemann Kleint committed
46 47 48 49
#ifdef Q_OS_WIN
#  include "peutils.h"
#endif

hjk's avatar
hjk committed
50
#include <projectexplorer/debugginghelper.h>
con's avatar
con committed
51
#include <projectexplorer/project.h>
52
#include <projectexplorer/toolchain.h>
con's avatar
con committed
53
#include <projectexplorer/projectexplorerconstants.h>
Tobias Hunger's avatar
Tobias Hunger committed
54
#include <projectexplorer/target.h>
55
#include <projectexplorer/buildconfiguration.h>
56
#include <projectexplorer/applicationrunconfiguration.h> // For LocalApplication*
con's avatar
con committed
57

58
#include <utils/synchronousprocess.h>
hjk's avatar
hjk committed
59
#include <utils/qtcassert.h>
60
#include <utils/fancymainwindow.h>
61
#include <coreplugin/icore.h>
hjk's avatar
hjk committed
62

con's avatar
con committed
63
#include <QtCore/QDir>
64
#include <QtGui/QMessageBox>
con's avatar
con committed
65

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

69
namespace Debugger {
hjk's avatar
hjk committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83

namespace Cdb {
DebuggerEngine *createCdbEngine(const DebuggerStartParameters &, QString *error);
bool isCdbEngineEnabled(); // Check the configuration page
}

/*
static QString toolChainName(int toolChainType)
{
    return ToolChain::toolChainName(ProjectExplorer::ToolChainType(toolChainType));
}
*/


84 85 86 87 88 89 90
namespace Internal {

DebuggerEngine *createGdbEngine(const DebuggerStartParameters &);
DebuggerEngine *createScriptEngine(const DebuggerStartParameters &);
DebuggerEngine *createPdbEngine(const DebuggerStartParameters &);
DebuggerEngine *createTcfEngine(const DebuggerStartParameters &);
DebuggerEngine *createQmlEngine(const DebuggerStartParameters &);
91
DebuggerEngine *createQmlCppEngine(const DebuggerStartParameters &);
hjk's avatar
hjk committed
92
DebuggerEngine *createLldbEngine(const DebuggerStartParameters &);
93

hjk's avatar
hjk committed
94
extern QString msgNoBinaryForToolChain(int tc);
95 96 97 98 99 100 101

// FIXME: Outdated?
// The createCdbEngine function takes a list of options pages it can add to.
// This allows for having a "enabled" toggle on the page independently
// of the engine. That's good for not enabling the related ActiveX control
// unnecessarily.

hjk's avatar
hjk committed
102
#ifdef CDB_ENABLED
hjk's avatar
hjk committed
103 104

DebuggerEngine *createCdbEngine(const DebuggerStartParameters &, QString *error);
105
bool checkCdbConfiguration(int toolChain, QString *errorMsg, QString *settingsPage);
106
bool isCdbEngineEnabled(); // Check the configuration page
hjk's avatar
hjk committed
107

hjk's avatar
hjk committed
108
#else
hjk's avatar
hjk committed
109 110 111 112 113 114 115 116 117 118 119

DebuggerEngine *createCdbEngine(const DebuggerStartParameters &, QString *)
{
    return 0;
}

bool checkCdbConfiguration(int, QString *, QString *)
{
    return false;
}

hjk's avatar
hjk committed
120
#endif
121

con's avatar
con committed
122 123
////////////////////////////////////////////////////////////////////////
//
124
// DebuggerRunControlFactory
con's avatar
con committed
125 126 127
//
////////////////////////////////////////////////////////////////////////

128 129 130 131 132 133
static QString msgEngineNotAvailable(const char *engine)
{
    return DebuggerPlugin::tr("The application requires the debugger engine '%1', "
        "which is disabled.").arg(QLatin1String(engine));
}

134
// A factory to create DebuggerRunControls
135
DebuggerRunControlFactory::DebuggerRunControlFactory(QObject *parent,
136
        unsigned enabledEngines)
137
    : IRunControlFactory(parent), m_enabledEngines(enabledEngines)
con's avatar
con committed
138 139
{}

140
bool DebuggerRunControlFactory::canRun(RunConfiguration *runConfiguration, const QString &mode) const
con's avatar
con committed
141
{
142
//    return mode == ProjectExplorer::Constants::DEBUGMODE;
143
    return mode == Constants::DEBUGMODE
144
            && qobject_cast<LocalApplicationRunConfiguration *>(runConfiguration);
con's avatar
con committed
145 146
}

147
QString DebuggerRunControlFactory::displayName() const
con's avatar
con committed
148
{
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
149
    return tr("Debug");
con's avatar
con committed
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
// Find Qt installation by running qmake
static inline QString findQtInstallPath(const QString &qmakePath)
{
    QProcess proc;
    QStringList args;
    args.append(QLatin1String("-query"));
    args.append(QLatin1String("QT_INSTALL_HEADERS"));
    proc.start(qmakePath, args);
    if (!proc.waitForStarted()) {
        qWarning("%s: Cannot start '%s': %s", Q_FUNC_INFO, qPrintable(qmakePath),
           qPrintable(proc.errorString()));
        return QString();
    }
    proc.closeWriteChannel();
    if (!proc.waitForFinished()) {
        Utils::SynchronousProcess::stopProcess(proc);
        qWarning("%s: Timeout running '%s'.", Q_FUNC_INFO, qPrintable(qmakePath));
        return QString();
    }
    if (proc.exitStatus() != QProcess::NormalExit) {
        qWarning("%s: '%s' crashed.", Q_FUNC_INFO, qPrintable(qmakePath));
        return QString();
    }
    const QByteArray ba = proc.readAllStandardOutput().trimmed();
    QDir dir(QString::fromLocal8Bit(ba));
    if (dir.exists() && dir.cdUp())
        return dir.absolutePath();
    return QString();
}

182
static DebuggerStartParameters localStartParameters(RunConfiguration *runConfiguration)
183
{
184
    DebuggerStartParameters sp;
185 186 187 188 189
    QTC_ASSERT(runConfiguration, return sp);
    LocalApplicationRunConfiguration *rc =
            qobject_cast<LocalApplicationRunConfiguration *>(runConfiguration);
    QTC_ASSERT(rc, return sp);

190
    sp.startMode = StartInternal;
191
    sp.environment = rc->environment();
192 193 194
    sp.workingDirectory = rc->workingDirectory();
    sp.executable = rc->executable();
    sp.processArgs = rc->commandLineArguments();
195 196 197 198
    sp.toolChainType = rc->toolChainType();
    sp.useTerminal = rc->runMode() == LocalApplicationRunConfiguration::Console;
    sp.dumperLibrary = rc->dumperLibrary();
    sp.dumperLibraryLocations = rc->dumperLibraryLocations();
199

200
    if (debuggerCore()->isActiveDebugLanguage(QmlLanguage)) {
201
        sp.qmlServerAddress = QLatin1String("127.0.0.1");
202
        sp.qmlServerPort = runConfiguration->qmlDebugServerPort();
203

204 205
        sp.projectDir = runConfiguration->target()->project()->projectDirectory();
        if (runConfiguration->target()->activeBuildConfiguration())
206 207
            sp.projectBuildDir = runConfiguration->target()
                ->activeBuildConfiguration()->buildDirectory();
208

209 210
        sp.processArgs.append(QLatin1String("-qmljsdebugger=port:")
            + QString::number(sp.qmlServerPort));
211 212
    }

213 214 215 216
    // FIXME: If it's not yet build this will be empty and not filled
    // when rebuild as the runConfiguration is not stored and therefore
    // cannot be used to retrieve the dumper location.
    //qDebug() << "DUMPER: " << sp.dumperLibrary << sp.dumperLibraryLocations;
217
    sp.displayName = rc->displayName();
218

219 220
    // Find qtInstallPath.
    QString qmakePath = DebuggingHelperLibrary::findSystemQt(rc->environment());
221 222
    if (!qmakePath.isEmpty())
        sp.qtInstallPath = findQtInstallPath(qmakePath);
223
    return sp;
224 225
}

226 227
RunControl *DebuggerRunControlFactory::create
    (RunConfiguration *runConfiguration, const QString &mode)
con's avatar
con committed
228
{
229
    QTC_ASSERT(mode == Constants::DEBUGMODE, return 0);
230
    DebuggerStartParameters sp = localStartParameters(runConfiguration);
231
    return create(sp, runConfiguration);
232 233
}

hjk's avatar
hjk committed
234 235
DebuggerRunControl *DebuggerRunControlFactory::create
    (const DebuggerStartParameters &sp, RunConfiguration *runConfiguration)
236
{
237 238
    DebuggerRunControl *runControl =
        new DebuggerRunControl(runConfiguration, m_enabledEngines, sp);
hjk's avatar
hjk committed
239 240 241 242 243
    if (runControl->engine())
        return runControl;
    qDebug() << "FAILED TO CREATE ENGINE";
    delete runControl;
    return 0;
con's avatar
con committed
244 245
}

246 247
QWidget *DebuggerRunControlFactory::createConfigurationWidget
    (RunConfiguration *runConfiguration)
con's avatar
con committed
248 249
{
    // NBS TODO: Add GDB-specific configuration widget
250
    Q_UNUSED(runConfiguration)
con's avatar
con committed
251 252 253
    return 0;
}

254 255
////////////////////////////////////////////////////////////////////////
//
hjk's avatar
hjk committed
256
// DebuggerRunControlPrivate
257 258 259
//
////////////////////////////////////////////////////////////////////////

hjk's avatar
hjk committed
260 261 262 263 264
class DebuggerRunControlPrivate
{
public:
    DebuggerRunControlPrivate(DebuggerRunControl *parent,
        RunConfiguration *runConfiguration, unsigned enabledEngines);
265
    unsigned enabledEngines() const;
266

hjk's avatar
hjk committed
267 268 269 270 271 272 273 274 275 276 277
    DebuggerEngineType engineForExecutable(unsigned enabledEngineTypes,
        const QString &executable);
    DebuggerEngineType engineForMode(unsigned enabledEngineTypes,
        DebuggerStartMode mode);

    void initGdbEngine(GdbEngine *engine);
    GdbEngine *gdbEngine() const;
    AbstractGdbAdapter *gdbAdapter() const;

public:
    DebuggerRunControl *q;
278 279 280
    DebuggerEngine *m_engine;
    const QWeakPointer<RunConfiguration> m_myRunConfiguration;
    bool m_running;
281
    const unsigned m_cmdLineEnabledEngines;
282 283 284 285
    QString m_errorMessage;
    QString m_settingsIdHint;
};

hjk's avatar
hjk committed
286
unsigned DebuggerRunControlPrivate::enabledEngines() const
287 288 289
{
    unsigned rc = m_cmdLineEnabledEngines;
#ifdef CDB_ENABLED
hjk's avatar
hjk committed
290
    if (!isCdbEngineEnabled() && !Cdb::isCdbEngineEnabled())
291 292 293 294 295
        rc &= ~CdbEngineType;
#endif
    return rc;
}

hjk's avatar
hjk committed
296 297 298 299
DebuggerRunControlPrivate::DebuggerRunControlPrivate(DebuggerRunControl *parent,
        RunConfiguration *runConfiguration, unsigned enabledEngines)
    : q(parent)
    , m_engine(0)
300
    , m_myRunConfiguration(runConfiguration)
301
    , m_running(false)
302
    , m_cmdLineEnabledEngines(enabledEngines)
303 304
{
}
305

306 307
// Figure out the debugger type of an executable. Analyze executable
// unless the toolchain provides a hint.
hjk's avatar
hjk committed
308 309
DebuggerEngineType DebuggerRunControlPrivate::engineForExecutable
    (unsigned enabledEngineTypes, const QString &executable)
310
{
311
    /*if (executable.endsWith(_("qmlviewer"))) {
312
        if (enabledEngineTypes & QmlEngineType)
313
            return QmlEngineType;
314
        d->m_errorMessage = msgEngineNotAvailable("Qml Engine");
315
    }*/
316

317
    if (executable.endsWith(_(".js"))) {
318
        if (enabledEngineTypes & ScriptEngineType)
319
            return ScriptEngineType;
hjk's avatar
hjk committed
320
        m_errorMessage = msgEngineNotAvailable("Script Engine");
321
    }
con's avatar
con committed
322

323
    if (executable.endsWith(_(".py"))) {
324
        if (enabledEngineTypes & PdbEngineType)
325
            return PdbEngineType;
hjk's avatar
hjk committed
326
        m_errorMessage = msgEngineNotAvailable("Pdb Engine");
327
    }
con's avatar
con committed
328

329 330 331
#ifdef Q_OS_WIN
    // A remote executable?
    if (!executable.endsWith(_(".exe")))
Friedemann Kleint's avatar
Friedemann Kleint committed
332
        return GdbEngineType;
333 334 335

    // If a file has PDB files, it has been compiled by VS.
    QStringList pdbFiles;
hjk's avatar
hjk committed
336
    if (!getPDBFiles(executable, &pdbFiles, &m_errorMessage)) {
337
        qWarning("Cannot determine type of executable %s: %s",
hjk's avatar
hjk committed
338
                 qPrintable(executable), qPrintable(m_errorMessage));
Friedemann Kleint's avatar
Friedemann Kleint committed
339
        return NoEngineType;
340 341 342
    }
    if (pdbFiles.empty())
        return GdbEngineType;
343

344
    // We need the CDB debugger in order to be able to debug VS
hjk's avatar
hjk committed
345
    // executables.
346
   if (DebuggerRunControl::checkDebugConfiguration(ProjectExplorer::ToolChain_MSVC,
hjk's avatar
hjk committed
347
            &m_errorMessage, 0, &m_settingsIdHint)) {
348 349
        if (enabledEngineTypes & CdbEngineType)
            return CdbEngineType;
hjk's avatar
hjk committed
350
        m_errorMessage = msgEngineNotAvailable("Cdb Engine");
351 352
        return NoEngineType;
    }
353
#else
354
    if (enabledEngineTypes & GdbEngineType)
355
        return GdbEngineType;
hjk's avatar
hjk committed
356
    m_errorMessage = msgEngineNotAvailable("Gdb Engine");
357
#endif
358

359
    return NoEngineType;
360 361
}

362
// Debugger type for mode.
hjk's avatar
hjk committed
363 364
DebuggerEngineType DebuggerRunControlPrivate::engineForMode
    (unsigned enabledEngineTypes, DebuggerStartMode startMode)
365
{
366 367
    if (startMode == AttachTcf)
        return TcfEngineType;
368

369 370
#ifdef Q_OS_WIN
    // Preferably Windows debugger for attaching locally.
371
    if (startMode != AttachToRemote && (enabledEngineTypes & CdbEngineType))
372
        return CdbEngineType;
373
    if (startMode == AttachCrashedExternal) {
374
        m_errorMessage = DebuggerRunControl::tr("There is no debugging engine available for post-mortem debugging.");
375 376
        return NoEngineType;
    }
Friedemann Kleint's avatar
Friedemann Kleint committed
377
    return GdbEngineType;
378 379
#else
    Q_UNUSED(startMode)
380
    Q_UNUSED(enabledEngineTypes)
hjk's avatar
hjk committed
381
    //  >m_errorMessage = msgEngineNotAvailable("Gdb Engine");
382 383
    return GdbEngineType;
#endif
384 385
}

hjk's avatar
hjk committed
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
void DebuggerRunControlPrivate::initGdbEngine(GdbEngine *engine)
{
    QTC_ASSERT(engine, return)

    // Forward adapter signals.
    AbstractGdbAdapter *adapter = engine->gdbAdapter();
    RemotePlainGdbAdapter *rpga = qobject_cast<RemotePlainGdbAdapter *>(adapter);
    RemoteGdbServerAdapter *rgsa = qobject_cast<RemoteGdbServerAdapter *>(adapter);
    if (rpga)
        q->connect(rpga, SIGNAL(requestSetup()), SIGNAL(engineRequestSetup()));
    else if (rgsa)
        q->connect(rgsa, SIGNAL(requestSetup()), SIGNAL(engineRequestSetup()));
}

GdbEngine *DebuggerRunControlPrivate::gdbEngine() const
{
    QTC_ASSERT(m_engine, return 0);
    if (GdbEngine *gdbEngine = qobject_cast<GdbEngine *>(m_engine))
        return gdbEngine;
    if (QmlCppEngine *qmlEngine = qobject_cast<QmlCppEngine *>(m_engine))
        if (GdbEngine *embeddedGdbEngine = qobject_cast<GdbEngine *>(qmlEngine->cppEngine()))
            return embeddedGdbEngine;
    return 0;
}

AbstractGdbAdapter *DebuggerRunControlPrivate::gdbAdapter() const
{
    GdbEngine *engine = gdbEngine();
    QTC_ASSERT(engine, return 0)
    return engine->gdbAdapter();
}

hjk's avatar
hjk committed
418 419 420
} // namespace Internal


hjk's avatar
hjk committed
421 422 423 424 425 426 427 428 429 430 431 432 433 434
////////////////////////////////////////////////////////////////////////
//
// DebuggerRunControl
//
////////////////////////////////////////////////////////////////////////

static DebuggerEngineType engineForToolChain(int toolChainType)
{
    switch (toolChainType) {
        case ProjectExplorer::ToolChain_LINUX_ICC:
        case ProjectExplorer::ToolChain_MinGW:
        case ProjectExplorer::ToolChain_GCC:
        case ProjectExplorer::ToolChain_WINSCW: // S60
        case ProjectExplorer::ToolChain_GCCE:
435 436
        case ProjectExplorer::ToolChain_RVCT2_ARMV5:
        case ProjectExplorer::ToolChain_RVCT2_ARMV6:
hjk's avatar
hjk committed
437 438 439
        case ProjectExplorer::ToolChain_RVCT_ARMV5_GNUPOC:
        case ProjectExplorer::ToolChain_GCCE_GNUPOC:
        case ProjectExplorer::ToolChain_GCC_MAEMO:
440 441 442 443 444
#ifdef WITH_LLDB
            // lldb override
            if (Core::ICore::instance()->settings()->value("LLDB/enabled").toBool())
                return LldbEngineType;
#endif
hjk's avatar
hjk committed
445 446
            return GdbEngineType;

447

hjk's avatar
hjk committed
448 449 450 451 452 453 454 455 456 457 458 459 460
        case ProjectExplorer::ToolChain_MSVC:
        case ProjectExplorer::ToolChain_WINCE:
            return CdbEngineType;

        case ProjectExplorer::ToolChain_OTHER:
        case ProjectExplorer::ToolChain_UNKNOWN:
        case ProjectExplorer::ToolChain_INVALID:
        default:
            break;
    }
    return NoEngineType;
}

hjk's avatar
hjk committed
461 462 463 464
DebuggerRunControl::DebuggerRunControl(RunConfiguration *runConfiguration,
        unsigned enabledEngines, const DebuggerStartParameters &startParams)
    : RunControl(runConfiguration, Constants::DEBUGMODE),
      d(new DebuggerRunControlPrivate(this, runConfiguration, enabledEngines))
465
{
hjk's avatar
hjk committed
466
    connect(this, SIGNAL(finished()), SLOT(handleFinished()));
467

468 469
    // Figure out engine according to toolchain, executable, attach or default.
    DebuggerEngineType engineType = NoEngineType;
470
    DebuggerLanguages activeLangs = debuggerCore()->activeLanguages();
hjk's avatar
hjk committed
471
    DebuggerStartParameters sp = startParams;
472
    const unsigned enabledEngineTypes = d->enabledEngines();
473
    if (sp.executable.endsWith(_(".js")))
474 475 476
        engineType = ScriptEngineType;
    else if (sp.executable.endsWith(_(".py")))
        engineType = PdbEngineType;
477
    else {
478
        engineType = engineForToolChain(sp.toolChainType);
479 480 481 482 483
        if (engineType == CdbEngineType && !(enabledEngineTypes & CdbEngineType)) {
            d->m_errorMessage = msgEngineNotAvailable("Cdb Engine");
            engineType = NoEngineType;
        }
    }
484

485 486 487
    // Fixme: unclean ipc override. Someone please have a better idea
    if (sp.startMode == StartRemoteEngine)
        // for now thats the only supported ipc engine
488 489
        engineType = LldbEngineType;

490
    // Fixme: 1 of 3 testing hacks.
491
    if (sp.processArgs.startsWith(__("@tcf@ ")))
492 493
        engineType = GdbEngineType;

494 495 496
    if (engineType == NoEngineType
            && sp.startMode != AttachToRemote
            && !sp.executable.isEmpty())
hjk's avatar
hjk committed
497
        engineType = d->engineForExecutable(enabledEngineTypes, sp.executable);
498

499
    if (engineType == NoEngineType)
hjk's avatar
hjk committed
500
        engineType = d->engineForMode(enabledEngineTypes, sp.startMode);
501

502 503
    if ((engineType != QmlEngineType && engineType != NoEngineType)
        && (activeLangs & QmlLanguage)) {
504
        if (activeLangs & CppLanguage) {
505 506 507 508 509 510 511
            sp.cppEngineType = engineType;
            engineType = QmlCppEngineType;
        } else {
            engineType = QmlEngineType;
        }
    }

Friedemann Kleint's avatar
Friedemann Kleint committed
512
    // qDebug() << "USING ENGINE : " << engineType;
513 514 515

    switch (engineType) {
        case GdbEngineType:
516
            d->m_engine = createGdbEngine(sp);
hjk's avatar
hjk committed
517
            d->initGdbEngine(qobject_cast<GdbEngine *>(d->m_engine));
518
            break;
519
        case ScriptEngineType:
hjk's avatar
hjk committed
520
            d->m_engine = createScriptEngine(sp);
521
            break;
522
        case CdbEngineType:
523
            // Try new engine, fall back to old.
hjk's avatar
hjk committed
524 525 526 527
            if (Cdb::isCdbEngineEnabled())
                d->m_engine = Cdb::createCdbEngine(sp, &d->m_errorMessage);
            else
                d->m_engine = Internal::createCdbEngine(sp, &d->m_errorMessage);
528
            break;
529
        case PdbEngineType:
hjk's avatar
hjk committed
530
            d->m_engine = createPdbEngine(sp);
531
            break;
532
        case TcfEngineType:
hjk's avatar
hjk committed
533
            d->m_engine = createTcfEngine(sp);
534
            break;
535
        case QmlEngineType:
hjk's avatar
hjk committed
536
            d->m_engine = createQmlEngine(sp);
537
            connect(qobject_cast<QmlEngine *>(d->m_engine),
hjk's avatar
hjk committed
538
                SIGNAL(remoteStartupRequested()),
539
                SIGNAL(engineRequestSetup()));
540
            break;
541
        case QmlCppEngineType:
hjk's avatar
hjk committed
542 543 544
            d->m_engine = createQmlCppEngine(sp);
            if (GdbEngine *embeddedGdbEngine = d->gdbEngine())
                d->initGdbEngine(embeddedGdbEngine);
545
            break;
hjk's avatar
hjk committed
546 547
        case LldbEngineType:
            d->m_engine = createLldbEngine(sp);
548 549
        case NoEngineType:
        case AllEngineTypes:
550
            break;
551 552 553 554 555
    }

    if (!d->m_engine) {
        // Could not find anything suitable.
        debuggingFinished();
hjk's avatar
hjk committed
556
        // Create Message box with possibility to go to settings.
557
        const QString msg = tr("Cannot debug '%1' (tool chain: '%2'): %3")
hjk's avatar
hjk committed
558
            .arg(sp.executable, sp.toolChainName(), d->m_errorMessage);
559
        Core::ICore::instance()->showWarningWithOptions(tr("Warning"),
hjk's avatar
hjk committed
560 561
            msg, QString(), QLatin1String(Constants::DEBUGGER_SETTINGS_CATEGORY),
            d->m_settingsIdHint);
562
    }
563 564
}

hjk's avatar
hjk committed
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
DebuggerRunControl::~DebuggerRunControl()
{
    disconnect();
    if (DebuggerEngine *engine = d->m_engine) {
        d->m_engine = 0;
        engine->disconnect();
        delete engine;
    }
}

const DebuggerStartParameters &DebuggerRunControl::startParameters() const
{
    QTC_ASSERT(d->m_engine, return *(new DebuggerStartParameters()));
    return d->m_engine->startParameters();
}

581
QString DebuggerRunControl::displayName() const
582
{
583 584
    QTC_ASSERT(d->m_engine, return QString());
    return d->m_engine->startParameters().displayName;
585 586
}

587
void DebuggerRunControl::setCustomEnvironment(Utils::Environment env)
588
{
589
    d->m_engine->startParameters().environment = env;
590 591
}

592 593 594 595 596 597 598 599 600 601 602 603 604
bool DebuggerRunControl::checkDebugConfiguration(int toolChain,
                                              QString *errorMessage,
                                              QString *settingsCategory /* = 0 */,
                                              QString *settingsPage /* = 0 */)
{
    errorMessage->clear();
    if (settingsCategory)
        settingsCategory->clear();
    if (settingsPage)
        settingsPage->clear();

    bool success = true;

605
    if (!(debuggerCore()->activeLanguages() & CppLanguage))
606 607
        return success;

608
    switch(toolChain) {
609 610 611 612 613 614
    case ProjectExplorer::ToolChain_GCC:
    case ProjectExplorer::ToolChain_LINUX_ICC:
    case ProjectExplorer::ToolChain_MinGW:
    case ProjectExplorer::ToolChain_WINCE: // S60
    case ProjectExplorer::ToolChain_WINSCW:
    case ProjectExplorer::ToolChain_GCCE:
615 616
    case ProjectExplorer::ToolChain_RVCT2_ARMV5:
    case ProjectExplorer::ToolChain_RVCT2_ARMV6:
hjk's avatar
hjk committed
617 618 619
        if (debuggerCore()->gdbBinaryForToolChain(toolChain).isEmpty()) {
            *errorMessage = msgNoBinaryForToolChain(toolChain);
            *settingsPage = GdbOptionsPage::settingsId();
620
            *errorMessage += msgEngineNotAvailable("Gdb");
hjk's avatar
hjk committed
621 622 623 624
            success = false;
        } else {
            success = true;
        }
625
        break;
626
    case ProjectExplorer::ToolChain_MSVC:
627 628
        success = checkCdbConfiguration(toolChain, errorMessage, settingsPage);
        if (!success) {
629
            *errorMessage += msgEngineNotAvailable("Cdb");
630 631 632 633 634 635 636
            if (settingsPage)
                *settingsPage = QLatin1String("Cdb");
        }
        break;
    }
    if (!success && settingsCategory && settingsPage && !settingsPage->isEmpty())
        *settingsCategory = QLatin1String(Constants::DEBUGGER_SETTINGS_CATEGORY);
hjk's avatar
hjk committed
637

638
    return success;
639 640
}

641
void DebuggerRunControl::start()
642
{
643 644
    QTC_ASSERT(d->m_engine, return);
    const DebuggerStartParameters &sp = d->m_engine->startParameters();
645

646 647 648
    QString errorMessage;
    QString settingsCategory;
    QString settingsPage;
649

650 651 652 653 654 655
    if (!checkDebugConfiguration(sp.toolChainType,
            &errorMessage, &settingsCategory, &settingsPage)) {
        emit appendMessage(this, errorMessage, true);
        emit finished();
        Core::ICore::instance()->showWarningWithOptions(tr("Debugger"),
            errorMessage, QString(), settingsCategory, settingsPage);
656
        return;
657
    }
658

659
    debuggerCore()->runControlStarted(this);
660

661 662
    // We might get a synchronous startFailed() notification on Windows,
    // when launching the process fails. Emit a proper finished() sequence.
663
    emit started();
664 665 666 667 668 669 670 671
    d->m_running = true;

    engine()->startDebugger(this);

    if (d->m_running) {
        emit addToOutputWindowInline(this, tr("Debugging starts"), false);
        emit addToOutputWindowInline(this, "\n", false);
    }
672 673
}

674
void DebuggerRunControl::startFailed()
675
{
676
    emit addToOutputWindowInline(this, tr("Debugging has failed"), false);
677
    d->m_running = false;
678
    emit finished();
679
    engine()->handleStartFailed();
680 681
}

682
void DebuggerRunControl::handleFinished()
683
{
684
    emit addToOutputWindowInline(this, tr("Debugging has finished"), false);
685 686
    if (engine())
        engine()->handleFinished();
687
    debuggerCore()->runControlFinished(this);
688 689
}

690
void DebuggerRunControl::showMessage(const QString &msg, int channel)
691
{
692 693 694 695 696 697 698 699 700 701 702
    switch (channel) {
        case AppOutput:
            emit addToOutputWindowInline(this, msg, false);
            break;
        case AppError:
            emit addToOutputWindowInline(this, msg, true);
            break;
        case AppStuff:
            emit appendMessage(this, msg, true);
            break;
    }
703 704
}

705
bool DebuggerRunControl::aboutToStop() const
706
{
707 708
    QTC_ASSERT(isRunning(), return true;)

Jarek Kobus's avatar
Jarek Kobus committed
709
    const QString question = tr("A debugging session is still in progress. "
710 711 712 713 714
            "Terminating the session in the current"
            " state can leave the target in an inconsistent state."
            " Would you still like to terminate it?");

    const QMessageBox::StandardButton answer =
715
            QMessageBox::question(debuggerCore()->mainWindow(),
716 717 718 719 720 721 722
                                  tr("Close Debugging Session"), question,
                                  QMessageBox::Yes|QMessageBox::No);
    return answer == QMessageBox::Yes;
}

RunControl::StopResult DebuggerRunControl::stop()
{
723 724
    QTC_ASSERT(d->m_engine, return StoppedSynchronously);
    d->m_engine->quitDebugger();
725
    return AsynchronousStop;
726 727
}

728
void DebuggerRunControl::debuggingFinished()
729
{
730
    d->m_running = false;
731
    emit finished();
732 733
}

734
bool DebuggerRunControl::isRunning() const
735
{
736
    return d->m_running;
737 738
}

739
DebuggerEngine *DebuggerRunControl::engine()
740
{
741 742
    QTC_ASSERT(d->m_engine, /**/);
    return d->m_engine;
743 744
}

745
void DebuggerRunControl::handleRemoteSetupDone(int gdbServerPort, int qmlPort)
746
{
hjk's avatar
hjk committed
747
    // FIXME: Use virtual functions?
748
    if (QmlEngine *qmlEngine = qobject_cast<QmlEngine *>(d->m_engine)) {
749
        qmlEngine->handleRemoteSetupDone(qmlPort);
hjk's avatar
hjk committed
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
        return;
    }

    AbstractGdbAdapter *adapter = d->gdbAdapter();
    QTC_ASSERT(adapter, return);

    RemotePlainGdbAdapter *rpga = qobject_cast<RemotePlainGdbAdapter *>(adapter);
    if (rpga) {
        rpga->handleSetupDone(qmlPort);
        return;
    }

    RemoteGdbServerAdapter *rgsa = qobject_cast<RemoteGdbServerAdapter *>(adapter);
    if (rgsa) {
        rgsa->handleSetupDone(gdbServerPort, qmlPort);
        return;
766
    }
hjk's avatar
hjk committed
767
    QTC_ASSERT(false, /**/);
768 769
}

770
void DebuggerRunControl::handleRemoteSetupFailed(const QString &message)
771
{
hjk's avatar
hjk committed
772
    // FIXME: Use virtual functions?
773 774
    if (QmlEngine *qmlEngine = qobject_cast<QmlEngine *>(d->m_engine)) {
        qmlEngine->handleRemoteSetupFailed(message);
hjk's avatar
hjk committed
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
        return;
    }

    AbstractGdbAdapter *adapter = d->gdbAdapter();
    QTC_ASSERT(adapter, return);

    RemotePlainGdbAdapter *rpga = qobject_cast<RemotePlainGdbAdapter *>(adapter);
    if (rpga) {
        rpga->handleSetupFailed(message);
        return;
    }

    RemoteGdbServerAdapter *rgsa = qobject_cast<RemoteGdbServerAdapter *>(adapter);
    if (rgsa) {
        rgsa->handleSetupFailed(message);
        return;
791
    }
hjk's avatar
hjk committed
792
    QTC_ASSERT(false, /**/);
793
}
794

795 796 797 798
RunConfiguration *DebuggerRunControl::runConfiguration() const
{
    return d->m_myRunConfiguration.data();
}
799

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