debuggerrunner.cpp 25.3 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) 2012 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
Eike Ziller's avatar
Eike Ziller committed
7
** Contact: http://www.qt-project.org/
con's avatar
con committed
8
**
9
**
10
** GNU Lesser General Public License Usage
11
**
hjk's avatar
hjk committed
12 13 14 15 16 17
** 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.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21 22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23 24 25 26 27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
**
29
**************************************************************************/
hjk's avatar
hjk committed
30

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

34
#include "debuggeractions.h"
35
#include "debuggerinternalconstants.h"
36
#include "debuggercore.h"
37
#include "debuggerengine.h"
38
#include "debuggermainwindow.h"
39 40
#include "debuggerplugin.h"
#include "debuggerstringutils.h"
Friedemann Kleint's avatar
Friedemann Kleint committed
41
#include "debuggerstartparameters.h"
Tobias Hunger's avatar
Tobias Hunger committed
42
#include "debuggerprofileinformation.h"
43
#include "lldb/lldbenginehost.h"
44
#include "debuggertooltipmanager.h"
45
#include "qml/qmlengine.h"
46

Friedemann Kleint's avatar
Friedemann Kleint committed
47 48
#ifdef Q_OS_WIN
#  include "peutils.h"
49
#  include <utils/winutils.h>
Friedemann Kleint's avatar
Friedemann Kleint committed
50 51
#endif

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

con's avatar
con committed
60
#include <utils/outputformat.h>
61
#include <utils/synchronousprocess.h>
hjk's avatar
hjk committed
62
#include <utils/qtcassert.h>
63
#include <utils/fancymainwindow.h>
64
#include <utils/qtcprocess.h>
65
#include <coreplugin/icore.h>
66
#include <coreplugin/helpmanager.h>
hjk's avatar
hjk committed
67

68
#include <QDir>
69 70
#include <QCheckBox>
#include <QSpinBox>
71 72
#include <QDebug>
#include <QErrorMessage>
73 74 75
#include <QFormLayout>
#include <QLabel>
#include <QMessageBox>
con's avatar
con committed
76

77
using namespace Debugger::Internal;
hjk's avatar
hjk committed
78 79
using namespace ProjectExplorer;
using namespace Utils;
80

81 82
enum { debug = 0 };

83
namespace Debugger {
84
namespace Internal {
85

hjk's avatar
hjk committed
86
bool isCdbEngineEnabled(); // Check the configuration page
hjk's avatar
hjk committed
87
//bool checkCdbConfiguration(const DebuggerStartParameters &sp, ConfigurationCheck *check);
hjk's avatar
hjk committed
88
DebuggerEngine *createCdbEngine(const DebuggerStartParameters &sp,
89
    DebuggerEngine *masterEngine, QString *error);
90

hjk's avatar
hjk committed
91
//bool checkGdbConfiguration(const DebuggerStartParameters &sp, ConfigurationCheck *check);
hjk's avatar
hjk committed
92
DebuggerEngine *createGdbEngine(const DebuggerStartParameters &sp,
93
    DebuggerEngine *masterEngine);
94

hjk's avatar
hjk committed
95 96
DebuggerEngine *createScriptEngine(const DebuggerStartParameters &sp);
DebuggerEngine *createPdbEngine(const DebuggerStartParameters &sp);
97
QmlEngine *createQmlEngine(const DebuggerStartParameters &sp,
98
    DebuggerEngine *masterEngine);
99
DebuggerEngine *createQmlCppEngine(const DebuggerStartParameters &sp,
hjk's avatar
hjk committed
100
    QString *errorMessage);
hjk's avatar
hjk committed
101
DebuggerEngine *createLldbEngine(const DebuggerStartParameters &sp);
102

hjk's avatar
hjk committed
103
extern QString msgNoBinaryForToolChain(const Abi &abi);
104

105 106 107 108 109 110
static const char *engineTypeName(DebuggerEngineType et)
{
    switch (et) {
    case Debugger::NoEngineType:
        break;
    case Debugger::GdbEngineType:
111
        return "Gdb engine";
112 113 114 115 116
    case Debugger::ScriptEngineType:
        return "Script engine";
    case Debugger::CdbEngineType:
        return "Cdb engine";
    case Debugger::PdbEngineType:
117
        return "Pdb engine";
118 119 120 121 122 123 124 125 126 127 128 129
    case Debugger::QmlEngineType:
        return "QML engine";
    case Debugger::QmlCppEngineType:
        return "QML C++ engine";
    case Debugger::LldbEngineType:
        return "LLDB engine";
    case Debugger::AllEngineTypes:
        break;
    }
    return "No engine";
}

130 131 132 133 134
static inline QString engineTypeNames(const QList<DebuggerEngineType> &l)
{
    QString rc;
    foreach (DebuggerEngineType et, l) {
        if (!rc.isEmpty())
135
            rc.append(QLatin1String(", "));
136 137 138 139 140
        rc += QLatin1String(engineTypeName(et));
    }
    return rc;
}

141 142 143
static QString msgEngineNotAvailable(const char *engine)
{
    return DebuggerPlugin::tr("The application requires the debugger engine '%1', "
144
        "which is disabled.").arg(_(engine));
145 146
}

147 148 149 150 151
static inline QString msgEngineNotAvailable(DebuggerEngineType et)
{
    return msgEngineNotAvailable(engineTypeName(et));
}

152 153 154 155 156 157
////////////////////////////////////////////////////////////////////////
//
// DebuggerRunConfigWidget
//
////////////////////////////////////////////////////////////////////////

hjk's avatar
hjk committed
158
class DebuggerRunConfigWidget : public RunConfigWidget
159 160 161 162
{
    Q_OBJECT

public:
163 164
    explicit DebuggerRunConfigWidget(RunConfiguration *runConfiguration);
    QString displayName() const { return tr("Debugger Settings"); }
165 166

private slots:
167 168
    void useCppDebuggerToggled(bool on);
    void useQmlDebuggerToggled(bool on);
169
    void qmlDebugServerPortChanged(int port);
170
    void useMultiProcessToggled(bool on);
171 172

public:
173
    DebuggerRunConfigurationAspect *m_aspect; // not owned
174 175 176 177 178 179

    QCheckBox *m_useCppDebugger;
    QCheckBox *m_useQmlDebugger;
    QSpinBox *m_debugServerPort;
    QLabel *m_debugServerPortLabel;
    QLabel *m_qmlDebuggerInfoLabel;
180
    QCheckBox *m_useMultiProcess;
181 182
};

183
DebuggerRunConfigWidget::DebuggerRunConfigWidget(RunConfiguration *runConfiguration)
184
{
185
    m_aspect = runConfiguration->debuggerAspect();
186

187 188
    m_useCppDebugger = new QCheckBox(tr("Enable C++"), this);
    m_useQmlDebugger = new QCheckBox(tr("Enable QML"), this);
189 190 191 192 193

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

194
    m_debugServerPortLabel = new QLabel(tr("Debug port:"), this);
195 196 197 198 199 200
    m_debugServerPortLabel->setBuddy(m_debugServerPort);

    m_qmlDebuggerInfoLabel = new QLabel(tr("<a href=\""
        "qthelp://com.nokia.qtcreator/doc/creator-debugging-qml.html"
        "\">What are the prerequisites?</a>"));

201 202 203
    m_useCppDebugger->setChecked(m_aspect->useCppDebugger());
    m_useQmlDebugger->setChecked(m_aspect->useQmlDebugger());

204
    m_debugServerPort->setValue(m_aspect->qmlDebugServerPort());
205

206 207 208 209 210 211
    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());

212
    connect(m_qmlDebuggerInfoLabel, SIGNAL(linkActivated(QString)),
213
            Core::HelpManager::instance(), SLOT(handleHelpRequest(QString)));
214
    connect(m_useQmlDebugger, SIGNAL(toggled(bool)),
215 216 217
            SLOT(useQmlDebuggerToggled(bool)));
    connect(m_useCppDebugger, SIGNAL(toggled(bool)),
            SLOT(useCppDebuggerToggled(bool)));
218
    connect(m_debugServerPort, SIGNAL(valueChanged(int)),
219
            SLOT(qmlDebugServerPortChanged(int)));
220 221
    connect(m_useMultiProcess, SIGNAL(toggled(bool)),
            SLOT(useMultiProcessToggled(bool)));
222

223 224 225
    if (m_aspect->isDisplaySuppressed())
        hide();

226
    if (m_aspect->areQmlDebuggingOptionsSuppressed()) {
227 228 229 230
        m_debugServerPortLabel->hide();
        m_debugServerPort->hide();
        m_useQmlDebugger->hide();
    }
231

232 233 234
    if (m_aspect->areCppDebuggingOptionsSuppressed())
        m_useCppDebugger->hide();

235 236 237 238 239
    if (m_aspect->isQmlDebuggingSpinboxSuppressed()) {
        m_debugServerPort->hide();
        m_debugServerPortLabel->hide();
    }

240 241 242 243 244 245 246 247 248 249 250 251
    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);
252
    layout->addWidget(m_useMultiProcess);
253 254 255
    setLayout(layout);
}

256
void DebuggerRunConfigWidget::qmlDebugServerPortChanged(int port)
257
{
258
    m_aspect->m_qmlDebugServerPort = port;
259 260
}

261
void DebuggerRunConfigWidget::useCppDebuggerToggled(bool on)
262
{
263 264
    m_aspect->m_useCppDebugger = on;
    if (!on && !m_useQmlDebugger->isChecked())
265 266 267
        m_useQmlDebugger->setChecked(true);
}

268
void DebuggerRunConfigWidget::useQmlDebuggerToggled(bool on)
269
{
270 271
    m_debugServerPort->setEnabled(on);
    m_debugServerPortLabel->setEnabled(on);
272

273
    m_aspect->m_useQmlDebugger = on
274 275
            ? DebuggerRunConfigurationAspect::EnableQmlDebugger
            : DebuggerRunConfigurationAspect::DisableQmlDebugger;
276
    if (!on && !m_useCppDebugger->isChecked())
277 278 279
        m_useCppDebugger->setChecked(true);
}

280 281 282 283 284
void DebuggerRunConfigWidget::useMultiProcessToggled(bool on)
{
    m_aspect->m_useMultiProcess = on;
}

285 286
////////////////////////////////////////////////////////////////////////
//
hjk's avatar
hjk committed
287
// DebuggerRunControlPrivate
288 289 290
//
////////////////////////////////////////////////////////////////////////

hjk's avatar
hjk committed
291 292 293
class DebuggerRunControlPrivate
{
public:
294 295
    explicit DebuggerRunControlPrivate(DebuggerRunControl *parent,
                                       RunConfiguration *runConfiguration);
296

hjk's avatar
hjk committed
297 298 299 300 301 302 303
    DebuggerEngineType engineForExecutable(unsigned enabledEngineTypes,
        const QString &executable);
    DebuggerEngineType engineForMode(unsigned enabledEngineTypes,
        DebuggerStartMode mode);

public:
    DebuggerRunControl *q;
304 305 306 307 308
    DebuggerEngine *m_engine;
    const QWeakPointer<RunConfiguration> m_myRunConfiguration;
    bool m_running;
};

hjk's avatar
hjk committed
309
DebuggerRunControlPrivate::DebuggerRunControlPrivate(DebuggerRunControl *parent,
310
                                                     RunConfiguration *runConfiguration)
hjk's avatar
hjk committed
311 312
    : q(parent)
    , m_engine(0)
313
    , m_myRunConfiguration(runConfiguration)
314
    , m_running(false)
315 316
{
}
317

hjk's avatar
hjk committed
318 319
} // namespace Internal

hjk's avatar
hjk committed
320
DebuggerRunControl::DebuggerRunControl(RunConfiguration *runConfiguration,
hjk's avatar
hjk committed
321
                                       const DebuggerStartParameters &sp)
hjk's avatar
hjk committed
322
    : RunControl(runConfiguration, DebugRunMode),
323
      d(new DebuggerRunControlPrivate(this, runConfiguration))
324
{
hjk's avatar
hjk committed
325
    connect(this, SIGNAL(finished()), SLOT(handleFinished()));
326 327 328
    // Create the engine. Could arguably be moved to the factory, but
    // we still have a derived S60DebugControl. Should rarely fail, though.
    QString errorMessage;
hjk's avatar
hjk committed
329 330
    d->m_engine = DebuggerRunControlFactory::createEngine(sp.masterEngineType, sp, 0, &errorMessage);

331 332 333
    if (d->m_engine) {
        DebuggerToolTipManager::instance()->registerEngine(d->m_engine);
    } else {
334
        debuggingFinished();
hjk's avatar
hjk committed
335
        Core::ICore::showWarningWithOptions(DebuggerRunControl::tr("Debugger"), errorMessage);
336
    }
337 338
}

hjk's avatar
hjk committed
339 340 341 342 343 344 345 346
DebuggerRunControl::~DebuggerRunControl()
{
    disconnect();
    if (DebuggerEngine *engine = d->m_engine) {
        d->m_engine = 0;
        engine->disconnect();
        delete engine;
    }
hjk's avatar
hjk committed
347
    delete d;
hjk's avatar
hjk committed
348 349 350 351 352 353 354 355
}

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

356
QString DebuggerRunControl::displayName() const
357
{
358 359
    QTC_ASSERT(d->m_engine, return QString());
    return d->m_engine->startParameters().displayName;
360 361
}

362 363
QIcon DebuggerRunControl::icon() const
{
364
    return QIcon(QLatin1String(ProjectExplorer::Constants::ICON_DEBUG_SMALL));
365 366
}

hjk's avatar
hjk committed
367
void DebuggerRunControl::setCustomEnvironment(Environment env)
368
{
369
    QTC_ASSERT(d->m_engine, return);
370
    d->m_engine->startParameters().environment = env;
371 372
}

373
void DebuggerRunControl::start()
374
{
375
    QTC_ASSERT(d->m_engine, return);
376 377 378
    // 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
379
        appendMessage(tr("No executable specified.\n"), ErrorMessageFormat);
380 381 382 383 384
        emit started();
        emit finished();
        return;
    }

385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
    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;
            }
401 402 403
        }
    }

404
    debuggerCore()->runControlStarted(d->m_engine);
405

406 407
    // We might get a synchronous startFailed() notification on Windows,
    // when launching the process fails. Emit a proper finished() sequence.
408
    emit started();
409 410
    d->m_running = true;

411
    d->m_engine->startDebugger(this);
412

413
    if (d->m_running)
hjk's avatar
hjk committed
414
        appendMessage(tr("Debugging starts\n"), NormalMessageFormat);
415 416
}

417
void DebuggerRunControl::startFailed()
418
{
hjk's avatar
hjk committed
419
    appendMessage(tr("Debugging has failed\n"), NormalMessageFormat);
420
    d->m_running = false;
421
    emit finished();
422
    d->m_engine->handleStartFailed();
423 424
}

425
void DebuggerRunControl::handleFinished()
426
{
hjk's avatar
hjk committed
427
    appendMessage(tr("Debugging has finished\n"), NormalMessageFormat);
428 429 430
    if (d->m_engine)
        d->m_engine->handleFinished();
    debuggerCore()->runControlFinished(d->m_engine);
431 432
}

433
void DebuggerRunControl::showMessage(const QString &msg, int channel)
434
{
435 436
    switch (channel) {
        case AppOutput:
hjk's avatar
hjk committed
437
            appendMessage(msg, StdOutFormatSameLine);
438 439
            break;
        case AppError:
hjk's avatar
hjk committed
440
            appendMessage(msg, StdErrFormatSameLine);
441 442
            break;
        case AppStuff:
hjk's avatar
hjk committed
443
            appendMessage(msg, DebugFormat);
444 445
            break;
    }
446 447
}

448
bool DebuggerRunControl::promptToStop(bool *optionalPrompt) const
449
{
450
    QTC_ASSERT(isRunning(), return true);
451

452 453 454
    if (optionalPrompt && !*optionalPrompt)
        return true;

Jarek Kobus's avatar
Jarek Kobus committed
455
    const QString question = tr("A debugging session is still in progress. "
456 457 458
            "Terminating the session in the current"
            " state can leave the target in an inconsistent state."
            " Would you still like to terminate it?");
459 460
    return showPromptToStopDialog(tr("Close Debugging Session"), question,
                                  QString(), QString(), optionalPrompt);
461 462 463 464
}

RunControl::StopResult DebuggerRunControl::stop()
{
465 466
    QTC_ASSERT(d->m_engine, return StoppedSynchronously);
    d->m_engine->quitDebugger();
467
    return AsynchronousStop;
468 469
}

470
void DebuggerRunControl::debuggingFinished()
471
{
472
    d->m_running = false;
473
    emit finished();
474 475
}

476
bool DebuggerRunControl::isRunning() const
477
{
478
    return d->m_running;
479 480
}

481
DebuggerEngine *DebuggerRunControl::engine()
482
{
483
    QTC_CHECK(d->m_engine);
484
    return d->m_engine;
485 486
}

487 488 489 490
RunConfiguration *DebuggerRunControl::runConfiguration() const
{
    return d->m_myRunConfiguration.data();
}
491

492 493 494 495 496 497 498 499

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

// A factory to create DebuggerRunControls
500 501
DebuggerRunControlFactory::DebuggerRunControlFactory(QObject *parent)
    : IRunControlFactory(parent)
502 503
{}

504
bool DebuggerRunControlFactory::canRun(RunConfiguration *runConfiguration, RunMode mode) const
505
{
506
    return (mode == DebugRunMode || mode == DebugRunModeWithBreakOnMain)
507 508 509 510 511
            && qobject_cast<LocalApplicationRunConfiguration *>(runConfiguration);
}

QString DebuggerRunControlFactory::displayName() const
{
hjk's avatar
hjk committed
512
    return DebuggerPlugin::tr("Debug");
513 514 515
}

// Find Qt installation by running qmake
hjk's avatar
hjk committed
516
static inline QString findQtInstallPath(const FileName &qmakePath)
517 518 519
{
    QProcess proc;
    QStringList args;
520 521
    args.append(_("-query"));
    args.append(_("QT_INSTALL_HEADERS"));
522
    proc.start(qmakePath.toString(), args);
523
    if (!proc.waitForStarted()) {
524
        qWarning("%s: Cannot start '%s': %s", Q_FUNC_INFO, qPrintable(qmakePath.toString()),
525 526 527 528 529
           qPrintable(proc.errorString()));
        return QString();
    }
    proc.closeWriteChannel();
    if (!proc.waitForFinished()) {
hjk's avatar
hjk committed
530
        SynchronousProcess::stopProcess(proc);
531
        qWarning("%s: Timeout running '%s'.", Q_FUNC_INFO, qPrintable(qmakePath.toString()));
532 533 534
        return QString();
    }
    if (proc.exitStatus() != QProcess::NormalExit) {
535
        qWarning("%s: '%s' crashed.", Q_FUNC_INFO, qPrintable(qmakePath.toString()));
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
        return QString();
    }
    const QByteArray ba = proc.readAllStandardOutput().trimmed();
    QDir dir(QString::fromLocal8Bit(ba));
    if (dir.exists() && dir.cdUp())
        return dir.absolutePath();
    return QString();
}

static DebuggerStartParameters localStartParameters(RunConfiguration *runConfiguration)
{
    DebuggerStartParameters sp;
    QTC_ASSERT(runConfiguration, return sp);
    LocalApplicationRunConfiguration *rc =
            qobject_cast<LocalApplicationRunConfiguration *>(runConfiguration);
    QTC_ASSERT(rc, return sp);

hjk's avatar
hjk committed
553 554
    Target *target = runConfiguration->target();
    Profile *profile = target ? target->profile() : ProfileManager::instance()->defaultProfile();
555
    fillParameters(&sp, profile ? profile->id() : Core::Id());
556 557
    sp.environment = rc->environment();
    sp.workingDirectory = rc->workingDirectory();
558 559 560

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

564
    sp.executable = rc->executable();
565 566 567
    if (sp.executable.isEmpty())
        return sp;
    sp.startMode = StartInternal;
568 569 570 571 572
    sp.processArgs = rc->commandLineArguments();
    sp.useTerminal = rc->runMode() == LocalApplicationRunConfiguration::Console;
    sp.dumperLibrary = rc->dumperLibrary();
    sp.dumperLibraryLocations = rc->dumperLibraryLocations();

hjk's avatar
hjk committed
573 574
    if (target) {
        if (const Project *project = target->project()) {
575
            sp.projectSourceDirectory = project->projectDirectory();
hjk's avatar
hjk committed
576
            if (const BuildConfiguration *buildConfig = target->activeBuildConfiguration())
577 578
                sp.projectBuildDirectory = buildConfig->buildDirectory();
            sp.projectSourceFiles = project->files(Project::ExcludeGeneratedFiles);
579 580 581
        }
    }

582 583 584 585
    DebuggerRunConfigurationAspect *aspect = runConfiguration->debuggerAspect();
    sp.multiProcess = aspect->useMultiProcess();

    if (aspect->useCppDebugger())
586 587
        sp.languages |= CppLanguage;

588
    if (aspect->useQmlDebugger()) {
589
        sp.qmlServerAddress = _("127.0.0.1");
590
        sp.qmlServerPort = aspect->qmlDebugServerPort();
591
        sp.languages |= QmlLanguage;
592

593 594
        // Makes sure that all bindings go through the JavaScript engine, so that
        // breakpoints are actually hit!
595
        const QString optimizerKey = _("QML_DISABLE_OPTIMIZER");
hjk's avatar
hjk committed
596
        if (!sp.environment.hasKey(optimizerKey))
597
            sp.environment.set(optimizerKey, _("1"));
598

hjk's avatar
hjk committed
599
        QtcProcess::addArg(&sp.processArgs, QString::fromLatin1("-qmljsdebugger=port:%1,block").arg(sp.qmlServerPort));
600 601 602 603 604 605 606 607 608 609 610 611
    }

    // 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
612
    (RunConfiguration *runConfiguration, RunMode mode)
613
{
614
    QTC_ASSERT(mode == DebugRunMode || mode == DebugRunModeWithBreakOnMain, return 0);
615
    DebuggerStartParameters sp = localStartParameters(runConfiguration);
616 617
    if (sp.startMode == NoStartMode)
        return 0;
618
    if (mode == DebugRunModeWithBreakOnMain)
619
        sp.breakOnMain = true;
hjk's avatar
hjk committed
620 621

    return doCreate(sp, runConfiguration);
622 623
}

hjk's avatar
hjk committed
624
static DebuggerEngineType guessUnixCppEngineType(const DebuggerStartParameters &sp)
625
{
hjk's avatar
hjk committed
626 627 628
    if (sp.debuggerCommand.contains(QLatin1String("lldb")))
        return LldbEngineType;
    return GdbEngineType;
629 630
}

hjk's avatar
hjk committed
631
static DebuggerEngineType guessCppEngineTypeForAbi(const DebuggerStartParameters &sp, const Abi &abi)
632
{
hjk's avatar
hjk committed
633 634 635 636 637 638 639 640 641 642 643 644 645
    switch (abi.binaryFormat()) {
        case Abi::ElfFormat:
        case Abi::MachOFormat:
            return guessUnixCppEngineType(sp) ;
        case Abi::PEFormat:
           if (abi.osFlavor() == Abi::WindowsMSysFlavor)
               return guessUnixCppEngineType(sp);
           return CdbEngineType;
       default:
           break;
    }
    return NoEngineType;
}
646

hjk's avatar
hjk committed
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
static DebuggerEngineType guessCppEngineType(const DebuggerStartParameters &sp, RunConfiguration *rc)
{
    if (rc) {
        if (Target *target = rc->target()) {
            Profile *profile = target->profile();
            if (ToolChain *tc = ToolChainProfileInformation::toolChain(profile)) {
                DebuggerEngineType et = guessCppEngineTypeForAbi(sp, tc->targetAbi());
                if (et != NoEngineType)
                    return et;
            }
        }
    }

    #ifdef Q_OS_WIN
    // If a file has PDB files, it has been compiled by VS.
    if (sp.executable.endsWith(_(".exe"), Qt::CaseInsensitive)) {
        QStringList pdbFiles;
        QString errorMessage;
        if (getPDBFiles(sp.executable, &pdbFiles, &errorMessage) && !pdbFiles.isEmpty())
            return CdbEngineType;
    }
    #endif

    QList<Abi> abis = Abi::abisOfBinary(FileName::fromString(sp.executable));
    foreach (const Abi &abi, abis) {
        DebuggerEngineType et = guessCppEngineTypeForAbi(sp, abi);
        if (et != NoEngineType)
            return et;
    }

    return guessUnixCppEngineType(sp);
}

static void fixupEngineTypes(DebuggerStartParameters &sp, RunConfiguration *rc)
{
    if (sp.masterEngineType != NoEngineType)
        return;

    if (sp.executable.endsWith(_(".js"))) {
        sp.masterEngineType = ScriptEngineType;
        return;
    }

    if (sp.executable.endsWith(_(".py"))) {
        sp.masterEngineType = PdbEngineType;
        return;
    }

    if (rc) {
        DebuggerRunConfigurationAspect *aspect = rc->debuggerAspect();
        bool useCppDebugger = aspect->useCppDebugger();
        bool useQmlDebugger = aspect->useQmlDebugger();
        if (useQmlDebugger) {
            if (useCppDebugger) {
                sp.masterEngineType = QmlCppEngineType;
                sp.firstSlaveEngineType = guessCppEngineType(sp, rc);
                sp.secondSlaveEngineType = QmlCppEngineType;
            } else {
                sp.masterEngineType = QmlEngineType;
            }
        } else {
            sp.masterEngineType = guessCppEngineType(sp, rc);
        }
        return;
    }

    sp.masterEngineType = guessCppEngineType(sp, rc);
}

DebuggerRunControl *DebuggerRunControlFactory::doCreate
    (const DebuggerStartParameters &sp0, RunConfiguration *rc)
{
    DebuggerStartParameters sp = sp0;
    if (!debuggerCore()->boolSetting(AutoEnrichParameters)) {
        const QString sysroot = sp.sysRoot;
        if (sp.debugInfoLocation.isEmpty()) {
            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"));
        }
733 734
    }

hjk's avatar
hjk committed
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
    fixupEngineTypes(sp, rc);
    if (!sp.masterEngineType)
        return 0;

    return new DebuggerRunControl(rc, sp);
}

DebuggerRunControl *DebuggerRunControlFactory::createAndScheduleRun
    (const DebuggerStartParameters &sp, RunConfiguration *runConfiguration)
{
    DebuggerRunControl *rc = doCreate(sp, runConfiguration);
    if (!rc)
        return 0;
    debuggerCore()->showMessage(sp.startMessage, 0);
    ProjectExplorerPlugin::instance()->startRunControl(rc, DebugRunMode);
    return rc;
}


RunConfigWidget *DebuggerRunControlFactory::createConfigurationWidget
    (RunConfiguration *runConfiguration)
{
    return new DebuggerRunConfigWidget(runConfiguration);
758 759
}

hjk's avatar
hjk committed
760 761 762 763 764
DebuggerEngine *DebuggerRunControlFactory::createEngine
    (DebuggerEngineType et,
     const DebuggerStartParameters &sp,
     DebuggerEngine *masterEngine,
     QString *errorMessage)
765 766 767 768 769 770 771 772 773 774 775 776 777 778
{
    switch (et) {
    case GdbEngineType:
        return createGdbEngine(sp, masterEngine);
    case ScriptEngineType:
        return createScriptEngine(sp);
    case CdbEngineType:
        return createCdbEngine(sp, masterEngine, errorMessage);
    case PdbEngineType:
        return createPdbEngine(sp);
    case QmlEngineType:
        return createQmlEngine(sp, masterEngine);
    case LldbEngineType:
        return createLldbEngine(sp);
hjk's avatar
hjk committed
779 780
    case QmlCppEngineType:
        return createQmlCppEngine(sp, errorMessage);
781 782
    default:
        break;
783
    }
hjk's avatar
hjk committed
784
    *errorMessage = DebuggerPlugin::tr("Unable to create a debugger engine of the type '%1'").
785
                    arg(_(engineTypeName(et)));
786 787 788
    return 0;
}

hjk's avatar
hjk committed
789
} // namespace Debugger
790 791

#include "debuggerrunner.moc"