debuggerrunner.cpp 18.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"
38
#include "debuggerrunconfigurationaspect.h"
Friedemann Kleint's avatar
Friedemann Kleint committed
39
#include "debuggerstartparameters.h"
40
#include "debuggerstringutils.h"
41
#include "debuggertooltipmanager.h"
42
#include "breakhandler.h"
43

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

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

hjk's avatar
hjk committed
57
#include <utils/qtcassert.h>
58
#include <utils/qtcprocess.h>
59
#include <coreplugin/icore.h>
hjk's avatar
hjk committed
60

61
#include <QErrorMessage>
62
#include <QTcpServer>
con's avatar
con committed
63

64
using namespace Debugger::Internal;
hjk's avatar
hjk committed
65
66
using namespace ProjectExplorer;
using namespace Utils;
67

68
69
enum { debug = 0 };

70
namespace Debugger {
71
namespace Internal {
72

73
74
DebuggerEngine *createCdbEngine(const DebuggerStartParameters &sp, QString *error);
DebuggerEngine *createGdbEngine(const DebuggerStartParameters &sp);
hjk's avatar
hjk committed
75
76
DebuggerEngine *createScriptEngine(const DebuggerStartParameters &sp);
DebuggerEngine *createPdbEngine(const DebuggerStartParameters &sp);
77
78
DebuggerEngine *createQmlEngine(const DebuggerStartParameters &sp);
DebuggerEngine *createQmlCppEngine(const DebuggerStartParameters &sp, QString *error);
79
DebuggerEngine *createLldbLibEngine(const DebuggerStartParameters &sp);
hjk's avatar
hjk committed
80
DebuggerEngine *createLldbEngine(const DebuggerStartParameters &sp);
81

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

109
110
////////////////////////////////////////////////////////////////////////
//
hjk's avatar
hjk committed
111
// DebuggerRunControlPrivate
112
113
114
//
////////////////////////////////////////////////////////////////////////

hjk's avatar
hjk committed
115
116
117
class DebuggerRunControlPrivate
{
public:
118
119
    explicit DebuggerRunControlPrivate(DebuggerRunControl *parent,
                                       RunConfiguration *runConfiguration);
120

hjk's avatar
hjk committed
121
122
public:
    DebuggerRunControl *q;
123
    DebuggerEngine *m_engine;
124
    const QPointer<RunConfiguration> m_myRunConfiguration;
125
126
127
    bool m_running;
};

hjk's avatar
hjk committed
128
DebuggerRunControlPrivate::DebuggerRunControlPrivate(DebuggerRunControl *parent,
129
                                                     RunConfiguration *runConfiguration)
hjk's avatar
hjk committed
130
131
    : q(parent)
    , m_engine(0)
132
    , m_myRunConfiguration(runConfiguration)
133
    , m_running(false)
134
135
{
}
136

hjk's avatar
hjk committed
137
138
} // namespace Internal

hjk's avatar
hjk committed
139
DebuggerRunControl::DebuggerRunControl(RunConfiguration *runConfiguration,
hjk's avatar
hjk committed
140
                                       const DebuggerStartParameters &sp)
hjk's avatar
hjk committed
141
    : RunControl(runConfiguration, DebugRunMode),
142
      d(new DebuggerRunControlPrivate(this, runConfiguration))
143
{
hjk's avatar
hjk committed
144
    connect(this, SIGNAL(finished()), SLOT(handleFinished()));
145
146
147
    // Create the engine. Could arguably be moved to the factory, but
    // we still have a derived S60DebugControl. Should rarely fail, though.
    QString errorMessage;
148
    d->m_engine = DebuggerRunControlFactory::createEngine(sp.masterEngineType, sp, &errorMessage);
hjk's avatar
hjk committed
149

150
151
152
    if (d->m_engine) {
        DebuggerToolTipManager::instance()->registerEngine(d->m_engine);
    } else {
153
        debuggingFinished();
hjk's avatar
hjk committed
154
        Core::ICore::showWarningWithOptions(DebuggerRunControl::tr("Debugger"), errorMessage);
155
    }
156
157
}

hjk's avatar
hjk committed
158
159
160
161
162
163
164
165
DebuggerRunControl::~DebuggerRunControl()
{
    disconnect();
    if (DebuggerEngine *engine = d->m_engine) {
        d->m_engine = 0;
        engine->disconnect();
        delete engine;
    }
hjk's avatar
hjk committed
166
    delete d;
hjk's avatar
hjk committed
167
168
169
170
171
172
173
174
}

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

175
QString DebuggerRunControl::displayName() const
176
{
177
178
    QTC_ASSERT(d->m_engine, return QString());
    return d->m_engine->startParameters().displayName;
179
180
}

181
182
QIcon DebuggerRunControl::icon() const
{
183
    return QIcon(QLatin1String(ProjectExplorer::Constants::ICON_DEBUG_SMALL));
184
185
}

hjk's avatar
hjk committed
186
void DebuggerRunControl::setCustomEnvironment(Environment env)
187
{
188
    QTC_ASSERT(d->m_engine, return);
189
    d->m_engine->startParameters().environment = env;
190
191
}

192
void DebuggerRunControl::start()
193
{
194
    QTC_ASSERT(d->m_engine, return);
195
196
197
    // 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
198
        appendMessage(tr("No executable specified.\n"), ErrorMessageFormat);
199
200
201
202
203
        emit started();
        emit finished();
        return;
    }

204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
    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;
            }
220
221
222
        }
    }

223
    debuggerCore()->runControlStarted(d->m_engine);
224

225
226
    // We might get a synchronous startFailed() notification on Windows,
    // when launching the process fails. Emit a proper finished() sequence.
227
    emit started();
228
229
    d->m_running = true;

230
    d->m_engine->startDebugger(this);
231

232
    if (d->m_running)
hjk's avatar
hjk committed
233
        appendMessage(tr("Debugging starts\n"), NormalMessageFormat);
234
235
}

236
void DebuggerRunControl::startFailed()
237
{
hjk's avatar
hjk committed
238
    appendMessage(tr("Debugging has failed\n"), NormalMessageFormat);
239
    d->m_running = false;
240
    emit finished();
241
    d->m_engine->handleStartFailed();
242
243
}

244
void DebuggerRunControl::handleFinished()
245
{
hjk's avatar
hjk committed
246
    appendMessage(tr("Debugging has finished\n"), NormalMessageFormat);
247
248
249
    if (d->m_engine)
        d->m_engine->handleFinished();
    debuggerCore()->runControlFinished(d->m_engine);
250
251
}

252
void DebuggerRunControl::showMessage(const QString &msg, int channel)
253
{
254
255
    switch (channel) {
        case AppOutput:
hjk's avatar
hjk committed
256
            appendMessage(msg, StdOutFormatSameLine);
257
258
            break;
        case AppError:
hjk's avatar
hjk committed
259
            appendMessage(msg, StdErrFormatSameLine);
260
261
            break;
        case AppStuff:
hjk's avatar
hjk committed
262
            appendMessage(msg, DebugFormat);
263
264
            break;
    }
265
266
}

267
bool DebuggerRunControl::promptToStop(bool *optionalPrompt) const
268
{
269
    QTC_ASSERT(isRunning(), return true);
270

271
272
273
    if (optionalPrompt && !*optionalPrompt)
        return true;

Jarek Kobus's avatar
Jarek Kobus committed
274
    const QString question = tr("A debugging session is still in progress. "
275
276
277
            "Terminating the session in the current"
            " state can leave the target in an inconsistent state."
            " Would you still like to terminate it?");
278
279
    return showPromptToStopDialog(tr("Close Debugging Session"), question,
                                  QString(), QString(), optionalPrompt);
280
281
282
283
}

RunControl::StopResult DebuggerRunControl::stop()
{
284
285
    QTC_ASSERT(d->m_engine, return StoppedSynchronously);
    d->m_engine->quitDebugger();
286
    return AsynchronousStop;
287
288
}

289
void DebuggerRunControl::debuggingFinished()
290
{
291
    d->m_running = false;
292
    emit finished();
293
294
}

295
bool DebuggerRunControl::isRunning() const
296
{
297
    return d->m_running;
298
299
}

300
DebuggerEngine *DebuggerRunControl::engine()
301
{
302
    QTC_CHECK(d->m_engine);
303
    return d->m_engine;
304
305
}

306
307
308
309
RunConfiguration *DebuggerRunControl::runConfiguration() const
{
    return d->m_myRunConfiguration.data();
}
310

311
312
313
314
315
316
317

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

318
319
DebuggerRunControlFactory::DebuggerRunControlFactory(QObject *parent)
    : IRunControlFactory(parent)
320
321
{}

322
bool DebuggerRunControlFactory::canRun(RunConfiguration *runConfiguration, RunMode mode) const
323
{
324
    return (mode == DebugRunMode || mode == DebugRunModeWithBreakOnMain)
325
326
327
            && qobject_cast<LocalApplicationRunConfiguration *>(runConfiguration);
}

328
static DebuggerStartParameters localStartParameters(RunConfiguration *runConfiguration, QString *errorMessage)
329
330
331
332
333
334
{
    DebuggerStartParameters sp;
    QTC_ASSERT(runConfiguration, return sp);
    LocalApplicationRunConfiguration *rc =
            qobject_cast<LocalApplicationRunConfiguration *>(runConfiguration);
    QTC_ASSERT(rc, return sp);
335
336
    EnvironmentAspect *environment = rc->extraAspect<ProjectExplorer::EnvironmentAspect>();
    QTC_ASSERT(environment, return sp);
337
338
    if (!rc->ensureConfigured(errorMessage))
        return sp;
339

hjk's avatar
hjk committed
340
    Target *target = runConfiguration->target();
Tobias Hunger's avatar
Tobias Hunger committed
341
    Kit *kit = target ? target->kit() : KitManager::instance()->defaultKit();
342
343
    if (!fillParameters(&sp, kit, errorMessage))
        return sp;
344
    sp.environment = environment->environment();
345
    sp.workingDirectory = rc->workingDirectory();
346
347
348

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

352
    sp.executable = rc->executable();
353
354
    if (sp.executable.isEmpty())
        return sp;
355

356
357
358
359
360
    sp.processArgs = rc->commandLineArguments();
    sp.useTerminal = rc->runMode() == LocalApplicationRunConfiguration::Console;
    sp.dumperLibrary = rc->dumperLibrary();
    sp.dumperLibraryLocations = rc->dumperLibraryLocations();

hjk's avatar
hjk committed
361
362
    if (target) {
        if (const Project *project = target->project()) {
363
            sp.projectSourceDirectory = project->projectDirectory();
hjk's avatar
hjk committed
364
            if (const BuildConfiguration *buildConfig = target->activeBuildConfiguration())
365
366
                sp.projectBuildDirectory = buildConfig->buildDirectory();
            sp.projectSourceFiles = project->files(Project::ExcludeGeneratedFiles);
367
368
369
        }
    }

370
    DebuggerRunConfigurationAspect *debugger
371
            = runConfiguration->extraAspect<Debugger::DebuggerRunConfigurationAspect>();
372
    sp.multiProcess = debugger->useMultiProcess();
373

374
    if (debugger->useCppDebugger())
375
376
        sp.languages |= CppLanguage;

377
    if (debugger->useQmlDebugger()) {
378
379
380
        const ProjectExplorer::IDevice::ConstPtr device =
                DeviceKitInformation::device(runConfiguration->target()->kit());
        QTC_ASSERT(device->type() == ProjectExplorer::Constants::DESKTOP_DEVICE_TYPE, return sp);
381
        QTcpServer server;
382
383
        const bool canListen = server.listen(QHostAddress::LocalHost)
                || server.listen(QHostAddress::LocalHostIPv6);
384
        if (!canListen) {
385
            if (errorMessage)
386
                *errorMessage = DebuggerPlugin::tr("Not enough free ports for QML debugging. ");
387
388
            return sp;
        }
389
        sp.qmlServerAddress = server.serverAddress().toString();
390
        sp.qmlServerPort = server.serverPort();
391
        sp.languages |= QmlLanguage;
392

393
394
        // Makes sure that all bindings go through the JavaScript engine, so that
        // breakpoints are actually hit!
395
        const QString optimizerKey = _("QML_DISABLE_OPTIMIZER");
hjk's avatar
hjk committed
396
        if (!sp.environment.hasKey(optimizerKey))
397
            sp.environment.set(optimizerKey, _("1"));
398

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

402
403
    sp.startMode = StartInternal;

404
405
406
407
408
409
410
411
412
413
    // 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
414
    (RunConfiguration *runConfiguration, RunMode mode, QString *errorMessage)
415
{
416
    QTC_ASSERT(mode == DebugRunMode || mode == DebugRunModeWithBreakOnMain, return 0);
417
    DebuggerStartParameters sp = localStartParameters(runConfiguration, errorMessage);
418
419
    if (sp.startMode == NoStartMode)
        return 0;
420
    if (mode == DebugRunModeWithBreakOnMain)
421
        sp.breakOnMain = true;
hjk's avatar
hjk committed
422

423
    return doCreate(sp, runConfiguration, errorMessage);
424
425
}

426
static bool fixupEngineTypes(DebuggerStartParameters &sp, RunConfiguration *rc, QString *errorMessage)
hjk's avatar
hjk committed
427
428
{
    if (sp.masterEngineType != NoEngineType)
429
        return true;
hjk's avatar
hjk committed
430
431
432

    if (sp.executable.endsWith(_(".js"))) {
        sp.masterEngineType = ScriptEngineType;
433
        return true;
hjk's avatar
hjk committed
434
435
436
437
    }

    if (sp.executable.endsWith(_(".py"))) {
        sp.masterEngineType = PdbEngineType;
438
        return true;
hjk's avatar
hjk committed
439
440
441
    }

    if (rc) {
442
        DebuggerRunConfigurationAspect *aspect
443
                = rc->extraAspect<Debugger::DebuggerRunConfigurationAspect>();
444
        if (const Target *target = rc->target())
445
446
            if (!fillParameters(&sp, target->kit(), errorMessage))
                return false;
447
448
        const bool useCppDebugger = aspect->useCppDebugger();
        const bool useQmlDebugger = aspect->useQmlDebugger();
hjk's avatar
hjk committed
449
450
451
        if (useQmlDebugger) {
            if (useCppDebugger) {
                sp.masterEngineType = QmlCppEngineType;
452
                sp.firstSlaveEngineType = sp.cppEngineType;
hjk's avatar
hjk committed
453
454
455
456
457
                sp.secondSlaveEngineType = QmlCppEngineType;
            } else {
                sp.masterEngineType = QmlEngineType;
            }
        } else {
458
            sp.masterEngineType = sp.cppEngineType;
hjk's avatar
hjk committed
459
        }
460
        return true;
hjk's avatar
hjk committed
461
    }
462
463
    sp.masterEngineType = sp.cppEngineType;
    return true;
hjk's avatar
hjk committed
464
465
466
}

DebuggerRunControl *DebuggerRunControlFactory::doCreate
467
    (const DebuggerStartParameters &sp0, RunConfiguration *rc, QString *errorMessage)
hjk's avatar
hjk committed
468
{
469
    TaskHub *th = ProjectExplorerPlugin::instance()->taskHub();
470
471
472
    th->clearTasks(Debugger::Constants::TASK_CATEGORY_DEBUGGER_DEBUGINFO);
    th->clearTasks(Debugger::Constants::TASK_CATEGORY_DEBUGGER_TEST);
    th->clearTasks(Debugger::Constants::TASK_CATEGORY_DEBUGGER_RUNTIME);
473

hjk's avatar
hjk committed
474
475
476
    DebuggerStartParameters sp = sp0;
    if (!debuggerCore()->boolSetting(AutoEnrichParameters)) {
        const QString sysroot = sp.sysRoot;
477
        if (sp.debugInfoLocation.isEmpty())
hjk's avatar
hjk committed
478
479
480
481
482
483
484
            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"));
        }
485
486
    }

487
    if (!fixupEngineTypes(sp, rc, errorMessage))
hjk's avatar
hjk committed
488
489
490
491
492
        return 0;

    return new DebuggerRunControl(rc, sp);
}

493
494
495
496
497
IRunConfigurationAspect *DebuggerRunControlFactory::createRunConfigurationAspect(RunConfiguration *rc)
{
    return new DebuggerRunConfigurationAspect(rc);
}

hjk's avatar
hjk committed
498
499
500
DebuggerRunControl *DebuggerRunControlFactory::createAndScheduleRun
    (const DebuggerStartParameters &sp, RunConfiguration *runConfiguration)
{
501
    QString errorMessage;
502
503
504
    if (runConfiguration && !runConfiguration->ensureConfigured(&errorMessage))
        ProjectExplorer::ProjectExplorerPlugin::showRunErrorMessage(errorMessage);

505
506
507
    DebuggerRunControl *rc = doCreate(sp, runConfiguration, &errorMessage);
    if (!rc) {
        ProjectExplorer::ProjectExplorerPlugin::showRunErrorMessage(errorMessage);
hjk's avatar
hjk committed
508
        return 0;
509
    }
hjk's avatar
hjk committed
510
511
512
513
514
    debuggerCore()->showMessage(sp.startMessage, 0);
    ProjectExplorerPlugin::instance()->startRunControl(rc, DebugRunMode);
    return rc;
}

515
516
DebuggerEngine *DebuggerRunControlFactory::createEngine(DebuggerEngineType et,
    const DebuggerStartParameters &sp, QString *errorMessage)
517
518
519
{
    switch (et) {
    case GdbEngineType:
520
        return createGdbEngine(sp);
521
522
523
    case ScriptEngineType:
        return createScriptEngine(sp);
    case CdbEngineType:
524
        return createCdbEngine(sp, errorMessage);
525
526
527
    case PdbEngineType:
        return createPdbEngine(sp);
    case QmlEngineType:
528
        return createQmlEngine(sp);
529
530
    case LldbEngineType:
        return createLldbEngine(sp);
531
532
    case LldbLibEngineType:
        return createLldbLibEngine(sp);
hjk's avatar
hjk committed
533
534
    case QmlCppEngineType:
        return createQmlCppEngine(sp, errorMessage);
535
536
    default:
        break;
537
    }
hjk's avatar
hjk committed
538
    *errorMessage = DebuggerPlugin::tr("Unable to create a debugger engine of the type '%1'").
539
                    arg(_(engineTypeName(et)));
540
541
542
    return 0;
}

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