debuggerrunner.cpp 22.6 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
**
con's avatar
con committed
9
** No Commercial Usage
10
**
con's avatar
con committed
11 12 13 14
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
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
**
con's avatar
con committed
25 26 27 28 29 30
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
con's avatar
con committed
31
**
32
**************************************************************************/
hjk's avatar
hjk committed
33

con's avatar
con committed
34 35
#include "debuggerrunner.h"

36
#include "debuggeractions.h"
37
#include "debuggercore.h"
38
#include "debuggerengine.h"
39
#include "debuggermainwindow.h"
40 41
#include "debuggerplugin.h"
#include "debuggerstringutils.h"
hjk's avatar
hjk committed
42
#include "gdb/gdboptionspage.h"
43
#include "lldb/lldbenginehost.h"
44

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

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

57
#include <utils/synchronousprocess.h>
hjk's avatar
hjk committed
58
#include <utils/qtcassert.h>
59
#include <utils/fancymainwindow.h>
60
#include <utils/qtcprocess.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

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

76 77 78 79 80 81 82
namespace Internal {

DebuggerEngine *createGdbEngine(const DebuggerStartParameters &);
DebuggerEngine *createScriptEngine(const DebuggerStartParameters &);
DebuggerEngine *createPdbEngine(const DebuggerStartParameters &);
DebuggerEngine *createTcfEngine(const DebuggerStartParameters &);
DebuggerEngine *createQmlEngine(const DebuggerStartParameters &);
83
DebuggerEngine *createQmlCppEngine(const DebuggerStartParameters &);
hjk's avatar
hjk committed
84
DebuggerEngine *createLldbEngine(const DebuggerStartParameters &);
85

hjk's avatar
hjk committed
86
extern QString msgNoBinaryForToolChain(int tc);
87 88 89 90 91 92 93

// 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
94
#ifdef CDB_ENABLED
hjk's avatar
hjk committed
95 96

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

hjk's avatar
hjk committed
100
#else
hjk's avatar
hjk committed
101 102 103 104 105 106 107 108 109 110 111

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

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

hjk's avatar
hjk committed
112
#endif
113 114 115 116 117 118 119

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

120 121
////////////////////////////////////////////////////////////////////////
//
hjk's avatar
hjk committed
122
// DebuggerRunControlPrivate
123 124 125
//
////////////////////////////////////////////////////////////////////////

hjk's avatar
hjk committed
126 127 128 129 130
class DebuggerRunControlPrivate
{
public:
    DebuggerRunControlPrivate(DebuggerRunControl *parent,
        RunConfiguration *runConfiguration, unsigned enabledEngines);
131
    unsigned enabledEngines() const;
132

hjk's avatar
hjk committed
133 134 135 136 137 138 139
    DebuggerEngineType engineForExecutable(unsigned enabledEngineTypes,
        const QString &executable);
    DebuggerEngineType engineForMode(unsigned enabledEngineTypes,
        DebuggerStartMode mode);

public:
    DebuggerRunControl *q;
140 141 142
    DebuggerEngine *m_engine;
    const QWeakPointer<RunConfiguration> m_myRunConfiguration;
    bool m_running;
143
    const unsigned m_cmdLineEnabledEngines;
144 145 146 147
    QString m_errorMessage;
    QString m_settingsIdHint;
};

hjk's avatar
hjk committed
148
unsigned DebuggerRunControlPrivate::enabledEngines() const
149 150 151
{
    unsigned rc = m_cmdLineEnabledEngines;
#ifdef CDB_ENABLED
hjk's avatar
hjk committed
152
    if (!isCdbEngineEnabled() && !Cdb::isCdbEngineEnabled())
153 154 155 156 157
        rc &= ~CdbEngineType;
#endif
    return rc;
}

hjk's avatar
hjk committed
158 159 160 161
DebuggerRunControlPrivate::DebuggerRunControlPrivate(DebuggerRunControl *parent,
        RunConfiguration *runConfiguration, unsigned enabledEngines)
    : q(parent)
    , m_engine(0)
162
    , m_myRunConfiguration(runConfiguration)
163
    , m_running(false)
164
    , m_cmdLineEnabledEngines(enabledEngines)
165 166
{
}
167

168 169
// Figure out the debugger type of an executable. Analyze executable
// unless the toolchain provides a hint.
hjk's avatar
hjk committed
170 171
DebuggerEngineType DebuggerRunControlPrivate::engineForExecutable
    (unsigned enabledEngineTypes, const QString &executable)
172
{
173
    if (executable.endsWith(_(".js"))) {
174
        if (enabledEngineTypes & ScriptEngineType)
175
            return ScriptEngineType;
hjk's avatar
hjk committed
176
        m_errorMessage = msgEngineNotAvailable("Script Engine");
177
    }
con's avatar
con committed
178

179
    if (executable.endsWith(_(".py"))) {
180
        if (enabledEngineTypes & PdbEngineType)
181
            return PdbEngineType;
hjk's avatar
hjk committed
182
        m_errorMessage = msgEngineNotAvailable("Pdb Engine");
183
    }
con's avatar
con committed
184

185 186 187
#ifdef Q_OS_WIN
    // A remote executable?
    if (!executable.endsWith(_(".exe")))
Friedemann Kleint's avatar
Friedemann Kleint committed
188
        return GdbEngineType;
189 190 191

    // If a file has PDB files, it has been compiled by VS.
    QStringList pdbFiles;
hjk's avatar
hjk committed
192
    if (!getPDBFiles(executable, &pdbFiles, &m_errorMessage)) {
193
        qWarning("Cannot determine type of executable %s: %s",
hjk's avatar
hjk committed
194
                 qPrintable(executable), qPrintable(m_errorMessage));
Friedemann Kleint's avatar
Friedemann Kleint committed
195
        return NoEngineType;
196 197 198
    }
    if (pdbFiles.empty())
        return GdbEngineType;
199

200
    // We need the CDB debugger in order to be able to debug VS
hjk's avatar
hjk committed
201
    // executables.
202
   if (DebuggerRunControl::checkDebugConfiguration(ProjectExplorer::ToolChain_MSVC,
hjk's avatar
hjk committed
203
            &m_errorMessage, 0, &m_settingsIdHint)) {
204 205
        if (enabledEngineTypes & CdbEngineType)
            return CdbEngineType;
hjk's avatar
hjk committed
206
        m_errorMessage = msgEngineNotAvailable("Cdb Engine");
207 208
        return NoEngineType;
    }
209
#else
210
    if (enabledEngineTypes & GdbEngineType)
211
        return GdbEngineType;
hjk's avatar
hjk committed
212
    m_errorMessage = msgEngineNotAvailable("Gdb Engine");
213
#endif
214

215
    return NoEngineType;
216 217
}

218
// Debugger type for mode.
hjk's avatar
hjk committed
219 220
DebuggerEngineType DebuggerRunControlPrivate::engineForMode
    (unsigned enabledEngineTypes, DebuggerStartMode startMode)
221
{
222 223
    if (startMode == AttachTcf)
        return TcfEngineType;
224

225 226
#ifdef Q_OS_WIN
    // Preferably Windows debugger for attaching locally.
227
    if (startMode != AttachToRemote && (enabledEngineTypes & CdbEngineType))
228
        return CdbEngineType;
229
    if (startMode == AttachCrashedExternal) {
230
        m_errorMessage = DebuggerRunControl::tr("There is no debugging engine available for post-mortem debugging.");
231 232
        return NoEngineType;
    }
Friedemann Kleint's avatar
Friedemann Kleint committed
233
    return GdbEngineType;
234 235
#else
    Q_UNUSED(startMode)
236
    Q_UNUSED(enabledEngineTypes)
hjk's avatar
hjk committed
237
    //  >m_errorMessage = msgEngineNotAvailable("Gdb Engine");
238 239
    return GdbEngineType;
#endif
240 241
}

hjk's avatar
hjk committed
242 243 244
} // namespace Internal


hjk's avatar
hjk committed
245 246 247 248 249 250 251 252 253 254 255 256 257 258
////////////////////////////////////////////////////////////////////////
//
// 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:
259 260
        case ProjectExplorer::ToolChain_RVCT2_ARMV5:
        case ProjectExplorer::ToolChain_RVCT2_ARMV6:
hjk's avatar
hjk committed
261 262 263
        case ProjectExplorer::ToolChain_RVCT_ARMV5_GNUPOC:
        case ProjectExplorer::ToolChain_GCCE_GNUPOC:
        case ProjectExplorer::ToolChain_GCC_MAEMO:
264 265 266 267 268
#ifdef WITH_LLDB
            // lldb override
            if (Core::ICore::instance()->settings()->value("LLDB/enabled").toBool())
                return LldbEngineType;
#endif
hjk's avatar
hjk committed
269 270
            return GdbEngineType;

271

hjk's avatar
hjk committed
272 273 274 275 276 277 278 279 280 281 282 283 284
        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
285 286 287 288
DebuggerRunControl::DebuggerRunControl(RunConfiguration *runConfiguration,
        unsigned enabledEngines, const DebuggerStartParameters &startParams)
    : RunControl(runConfiguration, Constants::DEBUGMODE),
      d(new DebuggerRunControlPrivate(this, runConfiguration, enabledEngines))
289
{
hjk's avatar
hjk committed
290
    connect(this, SIGNAL(finished()), SLOT(handleFinished()));
291

292 293
    // Figure out engine according to toolchain, executable, attach or default.
    DebuggerEngineType engineType = NoEngineType;
294
    DebuggerLanguages activeLangs = debuggerCore()->activeLanguages();
hjk's avatar
hjk committed
295
    DebuggerStartParameters sp = startParams;
296
    const unsigned enabledEngineTypes = d->enabledEngines();
297
    if (sp.executable.endsWith(_(".js")))
298 299 300
        engineType = ScriptEngineType;
    else if (sp.executable.endsWith(_(".py")))
        engineType = PdbEngineType;
301
    else {
302
        engineType = engineForToolChain(sp.toolChainType);
303 304 305 306 307
        if (engineType == CdbEngineType && !(enabledEngineTypes & CdbEngineType)) {
            d->m_errorMessage = msgEngineNotAvailable("Cdb Engine");
            engineType = NoEngineType;
        }
    }
308

hjk's avatar
hjk committed
309
    // FIXME: Unclean ipc override. Someone please have a better idea.
310
    if (sp.startMode == StartRemoteEngine)
hjk's avatar
hjk committed
311
        // For now thats the only supported IPC engine.
312 313
        engineType = LldbEngineType;

hjk's avatar
hjk committed
314
    // FIXME: 1 of 3 testing hacks.
315
    if (sp.processArgs.startsWith(__("@tcf@ ")))
316 317
        engineType = GdbEngineType;

318 319 320
    if (engineType == NoEngineType
            && sp.startMode != AttachToRemote
            && !sp.executable.isEmpty())
hjk's avatar
hjk committed
321
        engineType = d->engineForExecutable(enabledEngineTypes, sp.executable);
322

323
    if (engineType == NoEngineType)
hjk's avatar
hjk committed
324
        engineType = d->engineForMode(enabledEngineTypes, sp.startMode);
325

326 327
    if ((engineType != QmlEngineType && engineType != NoEngineType)
        && (activeLangs & QmlLanguage)) {
328
        if (activeLangs & CppLanguage) {
329 330 331 332 333 334 335
            sp.cppEngineType = engineType;
            engineType = QmlCppEngineType;
        } else {
            engineType = QmlEngineType;
        }
    }

Friedemann Kleint's avatar
Friedemann Kleint committed
336
    // qDebug() << "USING ENGINE : " << engineType;
337 338 339

    switch (engineType) {
        case GdbEngineType:
340
            d->m_engine = createGdbEngine(sp);
341
            break;
342
        case ScriptEngineType:
hjk's avatar
hjk committed
343
            d->m_engine = createScriptEngine(sp);
344
            break;
345
        case CdbEngineType:
346
            // Try new engine, fall back to old.
hjk's avatar
hjk committed
347 348 349 350
            if (Cdb::isCdbEngineEnabled())
                d->m_engine = Cdb::createCdbEngine(sp, &d->m_errorMessage);
            else
                d->m_engine = Internal::createCdbEngine(sp, &d->m_errorMessage);
351
            break;
352
        case PdbEngineType:
hjk's avatar
hjk committed
353
            d->m_engine = createPdbEngine(sp);
354
            break;
355
        case TcfEngineType:
hjk's avatar
hjk committed
356
            d->m_engine = createTcfEngine(sp);
357
            break;
358
        case QmlEngineType:
hjk's avatar
hjk committed
359
            d->m_engine = createQmlEngine(sp);
360
            break;
361
        case QmlCppEngineType:
hjk's avatar
hjk committed
362
            d->m_engine = createQmlCppEngine(sp);
363
            break;
hjk's avatar
hjk committed
364 365
        case LldbEngineType:
            d->m_engine = createLldbEngine(sp);
366 367
        case NoEngineType:
        case AllEngineTypes:
368
            break;
369 370 371 372 373
    }

    if (!d->m_engine) {
        // Could not find anything suitable.
        debuggingFinished();
hjk's avatar
hjk committed
374
        // Create Message box with possibility to go to settings.
hjk's avatar
hjk committed
375 376
        QString toolChainName =
            ToolChain::toolChainName(ProjectExplorer::ToolChainType(sp.toolChainType));
377
        const QString msg = tr("Cannot debug '%1' (tool chain: '%2'): %3")
hjk's avatar
hjk committed
378
            .arg(sp.executable, toolChainName, d->m_errorMessage);
379
        Core::ICore::instance()->showWarningWithOptions(tr("Warning"),
hjk's avatar
hjk committed
380 381
            msg, QString(), QLatin1String(Constants::DEBUGGER_SETTINGS_CATEGORY),
            d->m_settingsIdHint);
382
    }
383 384
}

hjk's avatar
hjk committed
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
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();
}

401
QString DebuggerRunControl::displayName() const
402
{
403 404
    QTC_ASSERT(d->m_engine, return QString());
    return d->m_engine->startParameters().displayName;
405 406
}

407
void DebuggerRunControl::setCustomEnvironment(Utils::Environment env)
408
{
409
    QTC_ASSERT(d->m_engine, return);
410
    d->m_engine->startParameters().environment = env;
411 412
}

413 414 415 416 417 418 419 420 421 422 423 424 425
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;

426
    if (!(debuggerCore()->activeLanguages() & CppLanguage))
427 428
        return success;

429
    switch(toolChain) {
430 431 432 433 434 435
    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:
436 437
    case ProjectExplorer::ToolChain_RVCT2_ARMV5:
    case ProjectExplorer::ToolChain_RVCT2_ARMV6:
hjk's avatar
hjk committed
438 439 440
        if (debuggerCore()->gdbBinaryForToolChain(toolChain).isEmpty()) {
            *errorMessage = msgNoBinaryForToolChain(toolChain);
            *settingsPage = GdbOptionsPage::settingsId();
441
            *errorMessage += msgEngineNotAvailable("Gdb");
hjk's avatar
hjk committed
442 443 444 445
            success = false;
        } else {
            success = true;
        }
446
        break;
447
    case ProjectExplorer::ToolChain_MSVC:
448 449
        success = checkCdbConfiguration(toolChain, errorMessage, settingsPage);
        if (!success) {
450
            *errorMessage += msgEngineNotAvailable("Cdb");
451 452 453 454 455 456 457
            if (settingsPage)
                *settingsPage = QLatin1String("Cdb");
        }
        break;
    }
    if (!success && settingsCategory && settingsPage && !settingsPage->isEmpty())
        *settingsCategory = QLatin1String(Constants::DEBUGGER_SETTINGS_CATEGORY);
hjk's avatar
hjk committed
458

459
    return success;
460 461
}

462
void DebuggerRunControl::start()
463
{
464
    QTC_ASSERT(d->m_engine, return);
465
    debuggerCore()->runControlStarted(d->m_engine);
466

467 468
    // We might get a synchronous startFailed() notification on Windows,
    // when launching the process fails. Emit a proper finished() sequence.
469
    emit started();
470 471
    d->m_running = true;

472
    d->m_engine->startDebugger(this);
473 474 475 476 477

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

480
void DebuggerRunControl::startFailed()
481
{
482
    emit addToOutputWindowInline(this, tr("Debugging has failed"), false);
483
    d->m_running = false;
484
    emit finished();
485
    d->m_engine->handleStartFailed();
486 487
}

488
void DebuggerRunControl::handleFinished()
489
{
490
    emit addToOutputWindowInline(this, tr("Debugging has finished"), false);
491 492 493
    if (d->m_engine)
        d->m_engine->handleFinished();
    debuggerCore()->runControlFinished(d->m_engine);
494 495
}

496
void DebuggerRunControl::showMessage(const QString &msg, int channel)
497
{
498 499 500 501 502 503 504 505 506 507 508
    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;
    }
509 510
}

511
bool DebuggerRunControl::aboutToStop() const
512
{
513 514
    QTC_ASSERT(isRunning(), return true;)

Jarek Kobus's avatar
Jarek Kobus committed
515
    const QString question = tr("A debugging session is still in progress. "
516 517 518 519 520
            "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 =
521
            QMessageBox::question(debuggerCore()->mainWindow(),
522 523 524 525 526 527 528
                                  tr("Close Debugging Session"), question,
                                  QMessageBox::Yes|QMessageBox::No);
    return answer == QMessageBox::Yes;
}

RunControl::StopResult DebuggerRunControl::stop()
{
529 530
    QTC_ASSERT(d->m_engine, return StoppedSynchronously);
    d->m_engine->quitDebugger();
531
    return AsynchronousStop;
532 533
}

534
void DebuggerRunControl::debuggingFinished()
535
{
536
    d->m_running = false;
537
    emit finished();
538 539
}

540
bool DebuggerRunControl::isRunning() const
541
{
542
    return d->m_running;
543 544
}

545
DebuggerEngine *DebuggerRunControl::engine()
546
{
547 548
    QTC_ASSERT(d->m_engine, /**/);
    return d->m_engine;
549 550
}

551 552 553 554
RunConfiguration *DebuggerRunControl::runConfiguration() const
{
    return d->m_myRunConfiguration.data();
}
555

556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637

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

// A factory to create DebuggerRunControls
DebuggerRunControlFactory::DebuggerRunControlFactory(QObject *parent,
        unsigned enabledEngines)
    : IRunControlFactory(parent), m_enabledEngines(enabledEngines)
{}

bool DebuggerRunControlFactory::canRun(RunConfiguration *runConfiguration, const QString &mode) const
{
//    return mode == ProjectExplorer::Constants::DEBUGMODE;
    return mode == Constants::DEBUGMODE
            && qobject_cast<LocalApplicationRunConfiguration *>(runConfiguration);
}

QString DebuggerRunControlFactory::displayName() const
{
    return tr("Debug");
}

// 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();
}

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

    sp.startMode = StartInternal;
    sp.environment = rc->environment();
    sp.workingDirectory = rc->workingDirectory();
    sp.executable = rc->executable();
    sp.processArgs = rc->commandLineArguments();
    sp.toolChainType = rc->toolChainType();
    sp.useTerminal = rc->runMode() == LocalApplicationRunConfiguration::Console;
    sp.dumperLibrary = rc->dumperLibrary();
    sp.dumperLibraryLocations = rc->dumperLibraryLocations();

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

        sp.projectDir = runConfiguration->target()->project()->projectDirectory();
        if (runConfiguration->target()->activeBuildConfiguration())
            sp.projectBuildDir = runConfiguration->target()
                ->activeBuildConfiguration()->buildDirectory();

638 639
        Utils::QtcProcess::addArg(&sp.processArgs, QLatin1String("-qmljsdebugger=port:")
                                  + QString::number(sp.qmlServerPort));
640 641 642 643 644 645 646 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
    }

    // 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();

    // Find qtInstallPath.
    QString qmakePath = DebuggingHelperLibrary::findSystemQt(rc->environment());
    if (!qmakePath.isEmpty())
        sp.qtInstallPath = findQtInstallPath(qmakePath);
    return sp;
}

RunControl *DebuggerRunControlFactory::create
    (RunConfiguration *runConfiguration, const QString &mode)
{
    QTC_ASSERT(mode == Constants::DEBUGMODE, return 0);
    DebuggerStartParameters sp = localStartParameters(runConfiguration);
    return create(sp, runConfiguration);
}

QWidget *DebuggerRunControlFactory::createConfigurationWidget
    (RunConfiguration *runConfiguration)
{
    // NBS TODO: Add GDB-specific configuration widget
    Q_UNUSED(runConfiguration)
    return 0;
}

DebuggerRunControl *DebuggerRunControlFactory::create
    (const DebuggerStartParameters &sp, RunConfiguration *runConfiguration)
{
674 675 676 677 678 679 680 681 682 683 684 685
    QString errorMessage;
    QString settingsCategory;
    QString settingsPage;

    if (!DebuggerRunControl::checkDebugConfiguration(sp.toolChainType,
            &errorMessage, &settingsCategory, &settingsPage)) {
        //emit appendMessage(this, errorMessage, true);
        Core::ICore::instance()->showWarningWithOptions(tr("Debugger"),
            errorMessage, QString(), settingsCategory, settingsPage);
        return 0;
    }

686 687 688 689 690 691 692 693
    DebuggerRunControl *runControl =
        new DebuggerRunControl(runConfiguration, m_enabledEngines, sp);
    if (runControl->d->m_engine)
        return runControl;
    delete runControl;
    return 0;
}

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