debuggerrunner.cpp 23.5 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
#include "debuggerrunner.h"
31
#include "debuggerruncontrolfactory.h"
con's avatar
con committed
32

33
#include "debuggeractions.h"
34
#include "debuggercore.h"
35 36
#include "debuggerengine.h"
#include "debuggerkitinformation.h"
37
#include "debuggerplugin.h"
Friedemann Kleint's avatar
Friedemann Kleint committed
38
#include "debuggerstartparameters.h"
39
#include "debuggerstringutils.h"
40
#include "debuggertooltipmanager.h"
41
#include "breakhandler.h"
42

Friedemann Kleint's avatar
Friedemann Kleint committed
43 44
#ifdef Q_OS_WIN
#  include "peutils.h"
45
#  include <utils/winutils.h>
Friedemann Kleint's avatar
Friedemann Kleint committed
46 47
#endif

48
#include <projectexplorer/localapplicationrunconfiguration.h> // For LocalApplication*
hjk's avatar
hjk committed
49
#include <projectexplorer/buildconfiguration.h>
con's avatar
con committed
50
#include <projectexplorer/project.h>
hjk's avatar
hjk committed
51
#include <projectexplorer/projectexplorer.h>
52
#include <projectexplorer/runconfiguration.h>
Tobias Hunger's avatar
Tobias Hunger committed
53
#include <projectexplorer/target.h>
54
#include <projectexplorer/taskhub.h>
con's avatar
con committed
55

hjk's avatar
hjk committed
56
#include <utils/qtcassert.h>
57
#include <utils/qtcprocess.h>
58 59
#include <utils/portlist.h>
#include <utils/tcpportsgatherer.h>
60
#include <coreplugin/icore.h>
61
#include <coreplugin/helpmanager.h>
hjk's avatar
hjk committed
62

63 64
#include <QCheckBox>
#include <QSpinBox>
65 66
#include <QDebug>
#include <QErrorMessage>
67 68
#include <QFormLayout>
#include <QLabel>
con's avatar
con committed
69

70
using namespace Debugger::Internal;
hjk's avatar
hjk committed
71 72
using namespace ProjectExplorer;
using namespace Utils;
73

74 75
enum { debug = 0 };

76
namespace Debugger {
77
namespace Internal {
78

79 80
DebuggerEngine *createCdbEngine(const DebuggerStartParameters &sp, QString *error);
DebuggerEngine *createGdbEngine(const DebuggerStartParameters &sp);
hjk's avatar
hjk committed
81 82
DebuggerEngine *createScriptEngine(const DebuggerStartParameters &sp);
DebuggerEngine *createPdbEngine(const DebuggerStartParameters &sp);
83 84
DebuggerEngine *createQmlEngine(const DebuggerStartParameters &sp);
DebuggerEngine *createQmlCppEngine(const DebuggerStartParameters &sp, QString *error);
85
DebuggerEngine *createLldbLibEngine(const DebuggerStartParameters &sp);
hjk's avatar
hjk committed
86
DebuggerEngine *createLldbEngine(const DebuggerStartParameters &sp);
87

88 89 90 91 92 93
static const char *engineTypeName(DebuggerEngineType et)
{
    switch (et) {
    case Debugger::NoEngineType:
        break;
    case Debugger::GdbEngineType:
94
        return "Gdb engine";
95 96 97 98 99
    case Debugger::ScriptEngineType:
        return "Script engine";
    case Debugger::CdbEngineType:
        return "Cdb engine";
    case Debugger::PdbEngineType:
100
        return "Pdb engine";
101 102 103 104
    case Debugger::QmlEngineType:
        return "QML engine";
    case Debugger::QmlCppEngineType:
        return "QML C++ engine";
105 106
    case Debugger::LldbLibEngineType:
        return "LLDB binary engine";
107
    case Debugger::LldbEngineType:
108
        return "LLDB command line engine";
109 110 111 112 113 114
    case Debugger::AllEngineTypes:
        break;
    }
    return "No engine";
}

115 116 117 118 119 120
////////////////////////////////////////////////////////////////////////
//
// DebuggerRunConfigWidget
//
////////////////////////////////////////////////////////////////////////

hjk's avatar
hjk committed
121
class DebuggerRunConfigWidget : public RunConfigWidget
122 123 124 125
{
    Q_OBJECT

public:
126 127
    explicit DebuggerRunConfigWidget(RunConfiguration *runConfiguration);
    QString displayName() const { return tr("Debugger Settings"); }
128 129

private slots:
130 131
    void useCppDebuggerToggled(bool on);
    void useQmlDebuggerToggled(bool on);
132
    void qmlDebugServerPortChanged(int port);
133
    void useMultiProcessToggled(bool on);
134 135

public:
136
    DebuggerRunConfigurationAspect *m_aspect; // not owned
137 138 139 140 141 142

    QCheckBox *m_useCppDebugger;
    QCheckBox *m_useQmlDebugger;
    QSpinBox *m_debugServerPort;
    QLabel *m_debugServerPortLabel;
    QLabel *m_qmlDebuggerInfoLabel;
143
    QCheckBox *m_useMultiProcess;
144 145
};

146
DebuggerRunConfigWidget::DebuggerRunConfigWidget(RunConfiguration *runConfiguration)
147
{
148
    m_aspect = runConfiguration->extraAspect<ProjectExplorer::DebuggerRunConfigurationAspect>();
149

150 151
    m_useCppDebugger = new QCheckBox(tr("Enable C++"), this);
    m_useQmlDebugger = new QCheckBox(tr("Enable QML"), this);
152 153 154 155 156

    m_debugServerPort = new QSpinBox(this);
    m_debugServerPort->setMinimum(1);
    m_debugServerPort->setMaximum(65535);

157
    m_debugServerPortLabel = new QLabel(tr("Debug port:"), this);
158 159 160
    m_debugServerPortLabel->setBuddy(m_debugServerPort);

    m_qmlDebuggerInfoLabel = new QLabel(tr("<a href=\""
161
        "qthelp://org.qt-project.qtcreator/doc/creator-debugging-qml.html"
162 163
        "\">What are the prerequisites?</a>"));

164 165 166
    m_useCppDebugger->setChecked(m_aspect->useCppDebugger());
    m_useQmlDebugger->setChecked(m_aspect->useQmlDebugger());

167
    m_debugServerPort->setValue(m_aspect->qmlDebugServerPort());
168

169 170 171 172 173 174
    static const QByteArray env = qgetenv("QTC_DEBUGGER_MULTIPROCESS");
    m_useMultiProcess =
        new QCheckBox(tr("Enable Debugging of Subprocesses"), this);
    m_useMultiProcess->setChecked(m_aspect->useMultiProcess());
    m_useMultiProcess->setVisible(env.toInt());

175
    connect(m_qmlDebuggerInfoLabel, SIGNAL(linkActivated(QString)),
176
            Core::HelpManager::instance(), SLOT(handleHelpRequest(QString)));
177
    connect(m_useQmlDebugger, SIGNAL(toggled(bool)),
178 179 180
            SLOT(useQmlDebuggerToggled(bool)));
    connect(m_useCppDebugger, SIGNAL(toggled(bool)),
            SLOT(useCppDebuggerToggled(bool)));
181
    connect(m_debugServerPort, SIGNAL(valueChanged(int)),
182
            SLOT(qmlDebugServerPortChanged(int)));
183 184
    connect(m_useMultiProcess, SIGNAL(toggled(bool)),
            SLOT(useMultiProcessToggled(bool)));
185

186 187 188
    if (m_aspect->isDisplaySuppressed())
        hide();

189
    if (m_aspect->areQmlDebuggingOptionsSuppressed()) {
190 191 192 193
        m_debugServerPortLabel->hide();
        m_debugServerPort->hide();
        m_useQmlDebugger->hide();
    }
194

195 196 197
    if (m_aspect->areCppDebuggingOptionsSuppressed())
        m_useCppDebugger->hide();

198 199 200 201 202
    if (m_aspect->isQmlDebuggingSpinboxSuppressed()) {
        m_debugServerPort->hide();
        m_debugServerPortLabel->hide();
    }

203 204 205 206 207 208 209 210 211 212 213 214
    QHBoxLayout *qmlLayout = new QHBoxLayout;
    qmlLayout->setMargin(0);
    qmlLayout->addWidget(m_useQmlDebugger);
    qmlLayout->addWidget(m_debugServerPortLabel);
    qmlLayout->addWidget(m_debugServerPort);
    qmlLayout->addWidget(m_qmlDebuggerInfoLabel);
    qmlLayout->addStretch();

    QVBoxLayout *layout = new QVBoxLayout;
    layout->setMargin(0);
    layout->addWidget(m_useCppDebugger);
    layout->addLayout(qmlLayout);
215
    layout->addWidget(m_useMultiProcess);
216 217 218
    setLayout(layout);
}

219
void DebuggerRunConfigWidget::qmlDebugServerPortChanged(int port)
220
{
221
    m_aspect->m_qmlDebugServerPort = port;
222 223
}

224
void DebuggerRunConfigWidget::useCppDebuggerToggled(bool on)
225
{
226 227
    m_aspect->m_useCppDebugger = on;
    if (!on && !m_useQmlDebugger->isChecked())
228 229 230
        m_useQmlDebugger->setChecked(true);
}

231
void DebuggerRunConfigWidget::useQmlDebuggerToggled(bool on)
232
{
233 234
    m_debugServerPort->setEnabled(on);
    m_debugServerPortLabel->setEnabled(on);
235

236
    m_aspect->m_useQmlDebugger = on
237 238
            ? DebuggerRunConfigurationAspect::EnableQmlDebugger
            : DebuggerRunConfigurationAspect::DisableQmlDebugger;
239
    if (!on && !m_useCppDebugger->isChecked())
240 241 242
        m_useCppDebugger->setChecked(true);
}

243 244 245 246 247
void DebuggerRunConfigWidget::useMultiProcessToggled(bool on)
{
    m_aspect->m_useMultiProcess = on;
}

248 249
////////////////////////////////////////////////////////////////////////
//
hjk's avatar
hjk committed
250
// DebuggerRunControlPrivate
251 252 253
//
////////////////////////////////////////////////////////////////////////

hjk's avatar
hjk committed
254 255 256
class DebuggerRunControlPrivate
{
public:
257 258
    explicit DebuggerRunControlPrivate(DebuggerRunControl *parent,
                                       RunConfiguration *runConfiguration);
259

hjk's avatar
hjk committed
260 261
public:
    DebuggerRunControl *q;
262
    DebuggerEngine *m_engine;
263
    const QPointer<RunConfiguration> m_myRunConfiguration;
264 265 266
    bool m_running;
};

hjk's avatar
hjk committed
267
DebuggerRunControlPrivate::DebuggerRunControlPrivate(DebuggerRunControl *parent,
268
                                                     RunConfiguration *runConfiguration)
hjk's avatar
hjk committed
269 270
    : q(parent)
    , m_engine(0)
271
    , m_myRunConfiguration(runConfiguration)
272
    , m_running(false)
273 274
{
}
275

hjk's avatar
hjk committed
276 277
} // namespace Internal

hjk's avatar
hjk committed
278
DebuggerRunControl::DebuggerRunControl(RunConfiguration *runConfiguration,
hjk's avatar
hjk committed
279
                                       const DebuggerStartParameters &sp)
hjk's avatar
hjk committed
280
    : RunControl(runConfiguration, DebugRunMode),
281
      d(new DebuggerRunControlPrivate(this, runConfiguration))
282
{
hjk's avatar
hjk committed
283
    connect(this, SIGNAL(finished()), SLOT(handleFinished()));
284 285 286
    // Create the engine. Could arguably be moved to the factory, but
    // we still have a derived S60DebugControl. Should rarely fail, though.
    QString errorMessage;
287
    d->m_engine = DebuggerRunControlFactory::createEngine(sp.masterEngineType, sp, &errorMessage);
hjk's avatar
hjk committed
288

289 290 291
    if (d->m_engine) {
        DebuggerToolTipManager::instance()->registerEngine(d->m_engine);
    } else {
292
        debuggingFinished();
hjk's avatar
hjk committed
293
        Core::ICore::showWarningWithOptions(DebuggerRunControl::tr("Debugger"), errorMessage);
294
    }
295 296
}

hjk's avatar
hjk committed
297 298 299 300 301 302 303 304
DebuggerRunControl::~DebuggerRunControl()
{
    disconnect();
    if (DebuggerEngine *engine = d->m_engine) {
        d->m_engine = 0;
        engine->disconnect();
        delete engine;
    }
hjk's avatar
hjk committed
305
    delete d;
hjk's avatar
hjk committed
306 307 308 309 310 311 312 313
}

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

314
QString DebuggerRunControl::displayName() const
315
{
316 317
    QTC_ASSERT(d->m_engine, return QString());
    return d->m_engine->startParameters().displayName;
318 319
}

320 321
QIcon DebuggerRunControl::icon() const
{
322
    return QIcon(QLatin1String(ProjectExplorer::Constants::ICON_DEBUG_SMALL));
323 324
}

hjk's avatar
hjk committed
325
void DebuggerRunControl::setCustomEnvironment(Environment env)
326
{
327
    QTC_ASSERT(d->m_engine, return);
328
    d->m_engine->startParameters().environment = env;
329 330
}

331
void DebuggerRunControl::start()
332
{
333
    QTC_ASSERT(d->m_engine, return);
334 335 336
    // User canceled input dialog asking for executable when working on library project.
    if (d->m_engine->startParameters().startMode == StartInternal
        && d->m_engine->startParameters().executable.isEmpty()) {
hjk's avatar
hjk committed
337
        appendMessage(tr("No executable specified.\n"), ErrorMessageFormat);
338 339 340 341 342
        emit started();
        emit finished();
        return;
    }

343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
    if (d->m_engine->startParameters().startMode == StartInternal) {
        foreach (const BreakpointModelId &id, debuggerCore()->breakHandler()->allBreakpointIds()) {
            if (d->m_engine->breakHandler()->breakpointData(id).enabled
                    && !d->m_engine->acceptsBreakpoint(id)) {

                QString warningMessage =
                        DebuggerPlugin::tr("Some breakpoints cannot be handled by the debugger "
                                           "languages currently active, and will be ignored.");

                debuggerCore()->showMessage(warningMessage, LogWarning);

                QErrorMessage *msgBox = new QErrorMessage(debuggerCore()->mainWindow());
                msgBox->setAttribute(Qt::WA_DeleteOnClose);
                msgBox->showMessage(warningMessage);
                break;
            }
359 360 361
        }
    }

362
    debuggerCore()->runControlStarted(d->m_engine);
363

364 365
    // We might get a synchronous startFailed() notification on Windows,
    // when launching the process fails. Emit a proper finished() sequence.
366
    emit started();
367 368
    d->m_running = true;

369
    d->m_engine->startDebugger(this);
370

371
    if (d->m_running)
hjk's avatar
hjk committed
372
        appendMessage(tr("Debugging starts\n"), NormalMessageFormat);
373 374
}

375
void DebuggerRunControl::startFailed()
376
{
hjk's avatar
hjk committed
377
    appendMessage(tr("Debugging has failed\n"), NormalMessageFormat);
378
    d->m_running = false;
379
    emit finished();
380
    d->m_engine->handleStartFailed();
381 382
}

383
void DebuggerRunControl::handleFinished()
384
{
hjk's avatar
hjk committed
385
    appendMessage(tr("Debugging has finished\n"), NormalMessageFormat);
386 387 388
    if (d->m_engine)
        d->m_engine->handleFinished();
    debuggerCore()->runControlFinished(d->m_engine);
389 390
}

391
void DebuggerRunControl::showMessage(const QString &msg, int channel)
392
{
393 394
    switch (channel) {
        case AppOutput:
hjk's avatar
hjk committed
395
            appendMessage(msg, StdOutFormatSameLine);
396 397
            break;
        case AppError:
hjk's avatar
hjk committed
398
            appendMessage(msg, StdErrFormatSameLine);
399 400
            break;
        case AppStuff:
hjk's avatar
hjk committed
401
            appendMessage(msg, DebugFormat);
402 403
            break;
    }
404 405
}

406
bool DebuggerRunControl::promptToStop(bool *optionalPrompt) const
407
{
408
    QTC_ASSERT(isRunning(), return true);
409

410 411 412
    if (optionalPrompt && !*optionalPrompt)
        return true;

Jarek Kobus's avatar
Jarek Kobus committed
413
    const QString question = tr("A debugging session is still in progress. "
414 415 416
            "Terminating the session in the current"
            " state can leave the target in an inconsistent state."
            " Would you still like to terminate it?");
417 418
    return showPromptToStopDialog(tr("Close Debugging Session"), question,
                                  QString(), QString(), optionalPrompt);
419 420 421 422
}

RunControl::StopResult DebuggerRunControl::stop()
{
423 424
    QTC_ASSERT(d->m_engine, return StoppedSynchronously);
    d->m_engine->quitDebugger();
425
    return AsynchronousStop;
426 427
}

428
void DebuggerRunControl::debuggingFinished()
429
{
430
    d->m_running = false;
431
    emit finished();
432 433
}

434
bool DebuggerRunControl::isRunning() const
435
{
436
    return d->m_running;
437 438
}

439
DebuggerEngine *DebuggerRunControl::engine()
440
{
441
    QTC_CHECK(d->m_engine);
442
    return d->m_engine;
443 444
}

445 446 447 448
RunConfiguration *DebuggerRunControl::runConfiguration() const
{
    return d->m_myRunConfiguration.data();
}
449

450 451 452 453 454 455 456

////////////////////////////////////////////////////////////////////////
//
// DebuggerRunControlFactory
//
////////////////////////////////////////////////////////////////////////

457 458
DebuggerRunControlFactory::DebuggerRunControlFactory(QObject *parent)
    : IRunControlFactory(parent)
459 460
{}

461
bool DebuggerRunControlFactory::canRun(RunConfiguration *runConfiguration, RunMode mode) const
462
{
463
    return (mode == DebugRunMode || mode == DebugRunModeWithBreakOnMain)
464 465 466 467 468
            && qobject_cast<LocalApplicationRunConfiguration *>(runConfiguration);
}

QString DebuggerRunControlFactory::displayName() const
{
hjk's avatar
hjk committed
469
    return DebuggerPlugin::tr("Debug");
470 471
}

472
static DebuggerStartParameters localStartParameters(RunConfiguration *runConfiguration, QString *errorMessage)
473 474 475 476 477 478
{
    DebuggerStartParameters sp;
    QTC_ASSERT(runConfiguration, return sp);
    LocalApplicationRunConfiguration *rc =
            qobject_cast<LocalApplicationRunConfiguration *>(runConfiguration);
    QTC_ASSERT(rc, return sp);
479 480
    if (!rc->ensureConfigured(errorMessage))
        return sp;
481

hjk's avatar
hjk committed
482
    Target *target = runConfiguration->target();
Tobias Hunger's avatar
Tobias Hunger committed
483
    Kit *kit = target ? target->kit() : KitManager::instance()->defaultKit();
484 485
    if (!fillParameters(&sp, kit, errorMessage))
        return sp;
486 487
    sp.environment = rc->environment();
    sp.workingDirectory = rc->workingDirectory();
488 489 490

#if defined(Q_OS_WIN)
    // Work around QTBUG-17529 (QtDeclarative fails with 'File name case mismatch' ...)
hjk's avatar
hjk committed
491
    sp.workingDirectory = normalizePathName(sp.workingDirectory);
492 493
#endif

494
    sp.executable = rc->executable();
495 496
    if (sp.executable.isEmpty())
        return sp;
497

498 499 500 501 502
    sp.processArgs = rc->commandLineArguments();
    sp.useTerminal = rc->runMode() == LocalApplicationRunConfiguration::Console;
    sp.dumperLibrary = rc->dumperLibrary();
    sp.dumperLibraryLocations = rc->dumperLibraryLocations();

hjk's avatar
hjk committed
503 504
    if (target) {
        if (const Project *project = target->project()) {
505
            sp.projectSourceDirectory = project->projectDirectory();
hjk's avatar
hjk committed
506
            if (const BuildConfiguration *buildConfig = target->activeBuildConfiguration())
507 508
                sp.projectBuildDirectory = buildConfig->buildDirectory();
            sp.projectSourceFiles = project->files(Project::ExcludeGeneratedFiles);
509 510 511
        }
    }

512 513
    DebuggerRunConfigurationAspect *aspect
            = runConfiguration->extraAspect<ProjectExplorer::DebuggerRunConfigurationAspect>();
514 515 516
    sp.multiProcess = aspect->useMultiProcess();

    if (aspect->useCppDebugger())
517 518
        sp.languages |= CppLanguage;

519
    if (aspect->useQmlDebugger()) {
520 521
        const ProjectExplorer::IDevice::ConstPtr device =
                DeviceKitInformation::device(runConfiguration->target()->kit());
522
        sp.qmlServerAddress = _("127.0.0.1");
523 524 525 526 527 528 529 530 531 532 533 534 535
        QTC_ASSERT(device->type() == ProjectExplorer::Constants::DESKTOP_DEVICE_TYPE, return sp);
        TcpPortsGatherer portsGatherer;
        portsGatherer.update(QAbstractSocket::UnknownNetworkLayerProtocol);
        Utils::PortList portList = device->freePorts();
        int freePort = portsGatherer.getNextFreePort(&portList);
        if (freePort == -1) {
            if (errorMessage)
                *errorMessage = DebuggerPlugin::tr("Not enough free ports for QML debugging. "
                                                   "Increase the port range for Desktop device in "
                                                   "Device settings.");
            return sp;
        }
        sp.qmlServerPort = freePort;
536
        sp.languages |= QmlLanguage;
537

538 539
        // Makes sure that all bindings go through the JavaScript engine, so that
        // breakpoints are actually hit!
540
        const QString optimizerKey = _("QML_DISABLE_OPTIMIZER");
hjk's avatar
hjk committed
541
        if (!sp.environment.hasKey(optimizerKey))
542
            sp.environment.set(optimizerKey, _("1"));
543

hjk's avatar
hjk committed
544
        QtcProcess::addArg(&sp.processArgs, QString::fromLatin1("-qmljsdebugger=port:%1,block").arg(sp.qmlServerPort));
545 546
    }

547 548
    sp.startMode = StartInternal;

549 550 551 552 553 554 555 556 557 558
    // 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;
    sp.displayName = rc->displayName();

    return sp;
}

RunControl *DebuggerRunControlFactory::create
559
    (RunConfiguration *runConfiguration, RunMode mode, QString *errorMessage)
560
{
561
    QTC_ASSERT(mode == DebugRunMode || mode == DebugRunModeWithBreakOnMain, return 0);
562
    DebuggerStartParameters sp = localStartParameters(runConfiguration, errorMessage);
563 564
    if (sp.startMode == NoStartMode)
        return 0;
565
    if (mode == DebugRunModeWithBreakOnMain)
566
        sp.breakOnMain = true;
hjk's avatar
hjk committed
567

568
    return doCreate(sp, runConfiguration, errorMessage);
569 570
}

571
static bool fixupEngineTypes(DebuggerStartParameters &sp, RunConfiguration *rc, QString *errorMessage)
hjk's avatar
hjk committed
572 573
{
    if (sp.masterEngineType != NoEngineType)
574
        return true;
hjk's avatar
hjk committed
575 576 577

    if (sp.executable.endsWith(_(".js"))) {
        sp.masterEngineType = ScriptEngineType;
578
        return true;
hjk's avatar
hjk committed
579 580 581 582
    }

    if (sp.executable.endsWith(_(".py"))) {
        sp.masterEngineType = PdbEngineType;
583
        return true;
hjk's avatar
hjk committed
584 585 586
    }

    if (rc) {
587 588
        DebuggerRunConfigurationAspect *aspect
                = rc->extraAspect<ProjectExplorer::DebuggerRunConfigurationAspect>();
589
        if (const Target *target = rc->target())
590 591
            if (!fillParameters(&sp, target->kit(), errorMessage))
                return false;
592 593
        const bool useCppDebugger = aspect->useCppDebugger();
        const bool useQmlDebugger = aspect->useQmlDebugger();
hjk's avatar
hjk committed
594 595 596
        if (useQmlDebugger) {
            if (useCppDebugger) {
                sp.masterEngineType = QmlCppEngineType;
597
                sp.firstSlaveEngineType = sp.cppEngineType;
hjk's avatar
hjk committed
598 599 600 601 602
                sp.secondSlaveEngineType = QmlCppEngineType;
            } else {
                sp.masterEngineType = QmlEngineType;
            }
        } else {
603
            sp.masterEngineType = sp.cppEngineType;
hjk's avatar
hjk committed
604
        }
605
        return true;
hjk's avatar
hjk committed
606
    }
607 608
    sp.masterEngineType = sp.cppEngineType;
    return true;
hjk's avatar
hjk committed
609 610 611
}

DebuggerRunControl *DebuggerRunControlFactory::doCreate
612
    (const DebuggerStartParameters &sp0, RunConfiguration *rc, QString *errorMessage)
hjk's avatar
hjk committed
613
{
614 615 616 617 618
    TaskHub *th = ProjectExplorerPlugin::instance()->taskHub();
    th->clearTasks(Core::Id(Debugger::Constants::TASK_CATEGORY_DEBUGGER_DEBUGINFO));
    th->clearTasks(Core::Id(Debugger::Constants::TASK_CATEGORY_DEBUGGER_TEST));
    th->clearTasks(Core::Id(Debugger::Constants::TASK_CATEGORY_DEBUGGER_RUNTIME));

hjk's avatar
hjk committed
619 620 621
    DebuggerStartParameters sp = sp0;
    if (!debuggerCore()->boolSetting(AutoEnrichParameters)) {
        const QString sysroot = sp.sysRoot;
622
        if (sp.debugInfoLocation.isEmpty())
hjk's avatar
hjk committed
623 624 625 626 627 628 629 630 631
            sp.debugInfoLocation = sysroot + QLatin1String("/usr/lib/debug");
        if (sp.debugSourceLocation.isEmpty()) {
            QString base = sysroot + QLatin1String("/usr/src/debug/");
            sp.debugSourceLocation.append(base + QLatin1String("qt5base/src/corelib"));
            sp.debugSourceLocation.append(base + QLatin1String("qt5base/src/gui"));
            sp.debugSourceLocation.append(base + QLatin1String("qt5base/src/network"));
            sp.debugSourceLocation.append(base + QLatin1String("qt5base/src/v8"));
            sp.debugSourceLocation.append(base + QLatin1String("qt5declarative/src/qml"));
        }
632 633
    }

634
    if (!fixupEngineTypes(sp, rc, errorMessage))
hjk's avatar
hjk committed
635 636 637 638 639
        return 0;

    return new DebuggerRunControl(rc, sp);
}

640 641 642 643 644
IRunConfigurationAspect *DebuggerRunControlFactory::createRunConfigurationAspect(RunConfiguration *rc)
{
    return new DebuggerRunConfigurationAspect(rc);
}

hjk's avatar
hjk committed
645 646 647
DebuggerRunControl *DebuggerRunControlFactory::createAndScheduleRun
    (const DebuggerStartParameters &sp, RunConfiguration *runConfiguration)
{
648
    QString errorMessage;
649 650 651
    if (runConfiguration && !runConfiguration->ensureConfigured(&errorMessage))
        ProjectExplorer::ProjectExplorerPlugin::showRunErrorMessage(errorMessage);

652 653 654
    DebuggerRunControl *rc = doCreate(sp, runConfiguration, &errorMessage);
    if (!rc) {
        ProjectExplorer::ProjectExplorerPlugin::showRunErrorMessage(errorMessage);
hjk's avatar
hjk committed
655
        return 0;
656
    }
hjk's avatar
hjk committed
657 658 659 660 661 662 663 664 665
    debuggerCore()->showMessage(sp.startMessage, 0);
    ProjectExplorerPlugin::instance()->startRunControl(rc, DebugRunMode);
    return rc;
}

RunConfigWidget *DebuggerRunControlFactory::createConfigurationWidget
    (RunConfiguration *runConfiguration)
{
    return new DebuggerRunConfigWidget(runConfiguration);
666 667
}

668 669
DebuggerEngine *DebuggerRunControlFactory::createEngine(DebuggerEngineType et,
    const DebuggerStartParameters &sp, QString *errorMessage)
670 671 672
{
    switch (et) {
    case GdbEngineType:
673
        return createGdbEngine(sp);
674 675 676
    case ScriptEngineType:
        return createScriptEngine(sp);
    case CdbEngineType:
677
        return createCdbEngine(sp, errorMessage);
678 679 680
    case PdbEngineType:
        return createPdbEngine(sp);
    case QmlEngineType:
681
        return createQmlEngine(sp);
682 683
    case LldbEngineType:
        return createLldbEngine(sp);
684 685
    case LldbLibEngineType:
        return createLldbLibEngine(sp);
hjk's avatar
hjk committed
686 687
    case QmlCppEngineType:
        return createQmlCppEngine(sp, errorMessage);
688 689
    default:
        break;
690
    }
hjk's avatar
hjk committed
691
    *errorMessage = DebuggerPlugin::tr("Unable to create a debugger engine of the type '%1'").
692
                    arg(_(engineTypeName(et)));
693 694 695
    return 0;
}

hjk's avatar
hjk committed
696
} // namespace Debugger
697 698

#include "debuggerrunner.moc"