debuggerrunner.cpp 23.1 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>
Tobias Hunger's avatar
Tobias Hunger committed
52
#include <projectexplorer/target.h>
53
#include <projectexplorer/taskhub.h>
con's avatar
con committed
54

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

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

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

73 74
enum { debug = 0 };

75
namespace Debugger {
76
namespace Internal {
77

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

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

114 115 116 117 118 119
////////////////////////////////////////////////////////////////////////
//
// DebuggerRunConfigWidget
//
////////////////////////////////////////////////////////////////////////

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

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

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

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

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

145
DebuggerRunConfigWidget::DebuggerRunConfigWidget(RunConfiguration *runConfiguration)
146
{
147
    m_aspect = runConfiguration->debuggerAspect();
148

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

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

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

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

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

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

168 169 170 171 172 173
    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());

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

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

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

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

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

202 203 204 205 206 207 208 209 210 211 212 213
    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);
214
    layout->addWidget(m_useMultiProcess);
215 216 217
    setLayout(layout);
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

330
void DebuggerRunControl::start()
331
{
332
    QTC_ASSERT(d->m_engine, return);
333 334 335
    // 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
336
        appendMessage(tr("No executable specified.\n"), ErrorMessageFormat);
337 338 339 340 341
        emit started();
        emit finished();
        return;
    }

342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
    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;
            }
358 359 360
        }
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

449 450 451 452 453 454 455

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

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

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

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

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

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

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

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

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

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

511 512 513 514
    DebuggerRunConfigurationAspect *aspect = runConfiguration->debuggerAspect();
    sp.multiProcess = aspect->useMultiProcess();

    if (aspect->useCppDebugger())
515 516
        sp.languages |= CppLanguage;

517
    if (aspect->useQmlDebugger()) {
518 519
        const ProjectExplorer::IDevice::ConstPtr device =
                DeviceKitInformation::device(runConfiguration->target()->kit());
520
        sp.qmlServerAddress = _("127.0.0.1");
521 522 523 524 525 526 527 528 529 530 531 532 533
        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;
534
        sp.languages |= QmlLanguage;
535

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

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

545 546
    sp.startMode = StartInternal;

547 548 549 550 551 552 553 554 555 556
    // 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
557
    (RunConfiguration *runConfiguration, RunMode mode, QString *errorMessage)
558
{
559
    QTC_ASSERT(mode == DebugRunMode || mode == DebugRunModeWithBreakOnMain, return 0);
560
    DebuggerStartParameters sp = localStartParameters(runConfiguration, errorMessage);
561 562
    if (sp.startMode == NoStartMode)
        return 0;
563
    if (mode == DebugRunModeWithBreakOnMain)
564
        sp.breakOnMain = true;
hjk's avatar
hjk committed
565

566
    return doCreate(sp, runConfiguration, errorMessage);
567 568
}

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

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

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

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

DebuggerRunControl *DebuggerRunControlFactory::doCreate
609
    (const DebuggerStartParameters &sp0, RunConfiguration *rc, QString *errorMessage)
hjk's avatar
hjk committed
610
{
611 612 613 614 615
    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
616 617 618
    DebuggerStartParameters sp = sp0;
    if (!debuggerCore()->boolSetting(AutoEnrichParameters)) {
        const QString sysroot = sp.sysRoot;
619
        if (sp.debugInfoLocation.isEmpty())
hjk's avatar
hjk committed
620 621 622 623 624 625 626 627 628
            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"));
        }
629 630
    }

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

    return new DebuggerRunControl(rc, sp);
}

DebuggerRunControl *DebuggerRunControlFactory::createAndScheduleRun
    (const DebuggerStartParameters &sp, RunConfiguration *runConfiguration)
{
640
    QString errorMessage;
641 642 643
    if (runConfiguration && !runConfiguration->ensureConfigured(&errorMessage))
        ProjectExplorer::ProjectExplorerPlugin::showRunErrorMessage(errorMessage);

644 645 646
    DebuggerRunControl *rc = doCreate(sp, runConfiguration, &errorMessage);
    if (!rc) {
        ProjectExplorer::ProjectExplorerPlugin::showRunErrorMessage(errorMessage);
hjk's avatar
hjk committed
647
        return 0;
648
    }
hjk's avatar
hjk committed
649 650 651 652 653 654 655 656 657
    debuggerCore()->showMessage(sp.startMessage, 0);
    ProjectExplorerPlugin::instance()->startRunControl(rc, DebugRunMode);
    return rc;
}

RunConfigWidget *DebuggerRunControlFactory::createConfigurationWidget
    (RunConfiguration *runConfiguration)
{
    return new DebuggerRunConfigWidget(runConfiguration);
658 659
}

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

hjk's avatar
hjk committed
688
} // namespace Debugger
689 690

#include "debuggerrunner.moc"