debuggerrunner.cpp 17.8 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2014 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"
hjk's avatar
hjk committed
43
#include "shared/peutils.h"
Friedemann Kleint's avatar
Friedemann Kleint committed
44

45
#include <projectexplorer/localapplicationrunconfiguration.h> // For LocalApplication*
46
#include <projectexplorer/environmentaspect.h> // For the environment
hjk's avatar
hjk committed
47
#include <projectexplorer/buildconfiguration.h>
con's avatar
con committed
48
#include <projectexplorer/project.h>
hjk's avatar
hjk committed
49
#include <projectexplorer/projectexplorer.h>
Tobias Hunger's avatar
Tobias Hunger committed
50
#include <projectexplorer/target.h>
51
#include <projectexplorer/taskhub.h>
con's avatar
con committed
52

Kai Koehne's avatar
Kai Koehne committed
53
#include <utils/checkablemessagebox.h>
Orgad Shaneh's avatar
Orgad Shaneh committed
54
#include <utils/fileutils.h>
hjk's avatar
hjk committed
55
#include <utils/qtcassert.h>
56
#include <utils/qtcprocess.h>
57
#include <coreplugin/icore.h>
hjk's avatar
hjk committed
58

59
#include <QTcpServer>
con's avatar
con committed
60

61
using namespace Debugger::Internal;
hjk's avatar
hjk committed
62
63
using namespace ProjectExplorer;
using namespace Utils;
64

65
66
enum { debug = 0 };

67
namespace Debugger {
68
namespace Internal {
69

70
71
DebuggerEngine *createCdbEngine(const DebuggerStartParameters &sp, QString *error);
DebuggerEngine *createGdbEngine(const DebuggerStartParameters &sp);
hjk's avatar
hjk committed
72
DebuggerEngine *createPdbEngine(const DebuggerStartParameters &sp);
73
74
DebuggerEngine *createQmlEngine(const DebuggerStartParameters &sp);
DebuggerEngine *createQmlCppEngine(const DebuggerStartParameters &sp, QString *error);
hjk's avatar
hjk committed
75
DebuggerEngine *createLldbEngine(const DebuggerStartParameters &sp);
76

77
78
79
80
81
82
static const char *engineTypeName(DebuggerEngineType et)
{
    switch (et) {
    case Debugger::NoEngineType:
        break;
    case Debugger::GdbEngineType:
83
        return "Gdb engine";
84
85
86
    case Debugger::CdbEngineType:
        return "Cdb engine";
    case Debugger::PdbEngineType:
87
        return "Pdb engine";
88
89
90
91
92
    case Debugger::QmlEngineType:
        return "QML engine";
    case Debugger::QmlCppEngineType:
        return "QML C++ engine";
    case Debugger::LldbEngineType:
93
        return "LLDB command line engine";
94
95
96
97
98
99
    case Debugger::AllEngineTypes:
        break;
    }
    return "No engine";
}

100
101
////////////////////////////////////////////////////////////////////////
//
hjk's avatar
hjk committed
102
// DebuggerRunControlPrivate
103
104
105
//
////////////////////////////////////////////////////////////////////////

hjk's avatar
hjk committed
106
107
108
class DebuggerRunControlPrivate
{
public:
109
110
    explicit DebuggerRunControlPrivate(DebuggerRunControl *parent,
                                       RunConfiguration *runConfiguration);
111

hjk's avatar
hjk committed
112
113
public:
    DebuggerRunControl *q;
114
    DebuggerEngine *m_engine;
115
    const QPointer<RunConfiguration> m_myRunConfiguration;
116
117
118
    bool m_running;
};

hjk's avatar
hjk committed
119
DebuggerRunControlPrivate::DebuggerRunControlPrivate(DebuggerRunControl *parent,
120
                                                     RunConfiguration *runConfiguration)
hjk's avatar
hjk committed
121
122
    : q(parent)
    , m_engine(0)
123
    , m_myRunConfiguration(runConfiguration)
124
    , m_running(false)
125
126
{
}
127

hjk's avatar
hjk committed
128
129
} // namespace Internal

hjk's avatar
hjk committed
130
DebuggerRunControl::DebuggerRunControl(RunConfiguration *runConfiguration,
hjk's avatar
hjk committed
131
                                       const DebuggerStartParameters &sp)
hjk's avatar
hjk committed
132
    : RunControl(runConfiguration, DebugRunMode),
133
      d(new DebuggerRunControlPrivate(this, runConfiguration))
134
{
hjk's avatar
hjk committed
135
    connect(this, SIGNAL(finished()), SLOT(handleFinished()));
136
137
138
    // Create the engine. Could arguably be moved to the factory, but
    // we still have a derived S60DebugControl. Should rarely fail, though.
    QString errorMessage;
139
    d->m_engine = DebuggerRunControlFactory::createEngine(sp.masterEngineType, sp, &errorMessage);
hjk's avatar
hjk committed
140

141
    if (d->m_engine) {
142
        DebuggerToolTipManager::registerEngine(d->m_engine);
143
    } else {
144
        debuggingFinished();
hjk's avatar
hjk committed
145
        Core::ICore::showWarningWithOptions(DebuggerRunControl::tr("Debugger"), errorMessage);
146
    }
147
148
}

hjk's avatar
hjk committed
149
150
151
152
153
154
155
156
DebuggerRunControl::~DebuggerRunControl()
{
    disconnect();
    if (DebuggerEngine *engine = d->m_engine) {
        d->m_engine = 0;
        engine->disconnect();
        delete engine;
    }
hjk's avatar
hjk committed
157
    delete d;
hjk's avatar
hjk committed
158
159
160
161
162
163
164
165
}

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

166
QString DebuggerRunControl::displayName() const
167
{
168
169
    QTC_ASSERT(d->m_engine, return QString());
    return d->m_engine->startParameters().displayName;
170
171
}

172
173
QIcon DebuggerRunControl::icon() const
{
174
    return QIcon(QLatin1String(ProjectExplorer::Constants::ICON_DEBUG_SMALL));
175
176
}

hjk's avatar
hjk committed
177
void DebuggerRunControl::setCustomEnvironment(Environment env)
178
{
179
    QTC_ASSERT(d->m_engine, return);
180
    d->m_engine->startParameters().environment = env;
181
182
}

183
void DebuggerRunControl::start()
184
{
185
    QTC_ASSERT(d->m_engine, return);
186
187
188
    // 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()) {
189
        appendMessage(tr("No executable specified.") + QLatin1Char('\n'), ErrorMessageFormat);
190
191
192
193
194
        emit started();
        emit finished();
        return;
    }

195
196
197
198
199
200
201
202
203
204
205
    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);

Kai Koehne's avatar
Kai Koehne committed
206
207
208
                static bool checked = true;
                if (!checked)
                    break;
209
                CheckableMessageBox::information(Core::ICore::mainWindow(),
Kai Koehne's avatar
Kai Koehne committed
210
211
212
213
                                                 tr("Debugger"),
                                                 warningMessage,
                                                 tr("&Show this message again."),
                                                 &checked, QDialogButtonBox::Ok);
214
215
                break;
            }
216
217
218
        }
    }

219
    debuggerCore()->runControlStarted(d->m_engine);
220

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

226
    d->m_engine->startDebugger(this);
227

228
    if (d->m_running)
229
        appendMessage(tr("Debugging starts") + QLatin1Char('\n'), NormalMessageFormat);
230
231
}

232
void DebuggerRunControl::startFailed()
233
{
234
    appendMessage(tr("Debugging has failed") + QLatin1Char('\n'), NormalMessageFormat);
235
    d->m_running = false;
236
    emit finished();
237
    d->m_engine->handleStartFailed();
238
239
}

240
void DebuggerRunControl::handleFinished()
241
{
242
    appendMessage(tr("Debugging has finished") + QLatin1Char('\n'), NormalMessageFormat);
243
244
245
    if (d->m_engine)
        d->m_engine->handleFinished();
    debuggerCore()->runControlFinished(d->m_engine);
246
247
}

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

263
bool DebuggerRunControl::promptToStop(bool *optionalPrompt) const
264
{
265
    QTC_ASSERT(isRunning(), return true);
266

267
268
269
    if (optionalPrompt && !*optionalPrompt)
        return true;

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

RunControl::StopResult DebuggerRunControl::stop()
{
280
281
    QTC_ASSERT(d->m_engine, return StoppedSynchronously);
    d->m_engine->quitDebugger();
282
    return AsynchronousStop;
283
284
}

285
void DebuggerRunControl::debuggingFinished()
286
{
287
    d->m_running = false;
288
    emit finished();
289
290
}

291
bool DebuggerRunControl::isRunning() const
292
{
293
    return d->m_running;
294
295
}

296
DebuggerEngine *DebuggerRunControl::engine()
297
{
298
    QTC_CHECK(d->m_engine);
299
    return d->m_engine;
300
301
}

302
303
304
305
RunConfiguration *DebuggerRunControl::runConfiguration() const
{
    return d->m_myRunConfiguration.data();
}
306

307
308
309
310
311
312
313

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

314
315
DebuggerRunControlFactory::DebuggerRunControlFactory(QObject *parent)
    : IRunControlFactory(parent)
316
317
{}

318
bool DebuggerRunControlFactory::canRun(RunConfiguration *runConfiguration, RunMode mode) const
319
{
320
    return (mode == DebugRunMode || mode == DebugRunModeWithBreakOnMain)
321
322
323
            && qobject_cast<LocalApplicationRunConfiguration *>(runConfiguration);
}

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

hjk's avatar
hjk committed
336
    Target *target = runConfiguration->target();
337
    Kit *kit = target ? target->kit() : KitManager::defaultKit();
338
339
    if (!fillParameters(&sp, kit, errorMessage))
        return sp;
340
    sp.environment = environment->environment();
341

Orgad Shaneh's avatar
Orgad Shaneh committed
342
343
    // Normalize to work around QTBUG-17529 (QtDeclarative fails with 'File name case mismatch'...)
    sp.workingDirectory = FileUtils::normalizePathName(rc->workingDirectory());
344

345
    sp.executable = rc->executable();
346
347
    if (sp.executable.isEmpty())
        return sp;
348

349
350
351
    sp.processArgs = rc->commandLineArguments();
    sp.useTerminal = rc->runMode() == LocalApplicationRunConfiguration::Console;

hjk's avatar
hjk committed
352
353
    if (target) {
        if (const Project *project = target->project()) {
354
            sp.projectSourceDirectory = project->projectDirectory();
hjk's avatar
hjk committed
355
            if (const BuildConfiguration *buildConfig = target->activeBuildConfiguration())
356
                sp.projectBuildDirectory = buildConfig->buildDirectory().toString();
357
            sp.projectSourceFiles = project->files(Project::ExcludeGeneratedFiles);
358
359
360
        }
    }

361
    DebuggerRunConfigurationAspect *debugger
362
            = runConfiguration->extraAspect<Debugger::DebuggerRunConfigurationAspect>();
363
    sp.multiProcess = debugger->useMultiProcess();
364

365
    if (debugger->useCppDebugger())
366
367
        sp.languages |= CppLanguage;

368
    if (debugger->useQmlDebugger()) {
369
370
371
        const ProjectExplorer::IDevice::ConstPtr device =
                DeviceKitInformation::device(runConfiguration->target()->kit());
        QTC_ASSERT(device->type() == ProjectExplorer::Constants::DESKTOP_DEVICE_TYPE, return sp);
372
        QTcpServer server;
373
374
        const bool canListen = server.listen(QHostAddress::LocalHost)
                || server.listen(QHostAddress::LocalHostIPv6);
375
        if (!canListen) {
376
            if (errorMessage)
377
                *errorMessage = DebuggerPlugin::tr("Not enough free ports for QML debugging.") + QLatin1Char(' ');
378
379
            return sp;
        }
380
        sp.qmlServerAddress = server.serverAddress().toString();
381
        sp.qmlServerPort = server.serverPort();
382
        sp.languages |= QmlLanguage;
383

384
385
        // Makes sure that all bindings go through the JavaScript engine, so that
        // breakpoints are actually hit!
386
        const QString optimizerKey = _("QML_DISABLE_OPTIMIZER");
hjk's avatar
hjk committed
387
        if (!sp.environment.hasKey(optimizerKey))
388
            sp.environment.set(optimizerKey, _("1"));
389

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

393
    sp.startMode = StartInternal;
394
395
396
397
398
399
    sp.displayName = rc->displayName();

    return sp;
}

RunControl *DebuggerRunControlFactory::create
400
    (RunConfiguration *runConfiguration, RunMode mode, QString *errorMessage)
401
{
402
    QTC_ASSERT(mode == DebugRunMode || mode == DebugRunModeWithBreakOnMain, return 0);
403
    DebuggerStartParameters sp = localStartParameters(runConfiguration, errorMessage);
404
405
    if (sp.startMode == NoStartMode)
        return 0;
406
    if (mode == DebugRunModeWithBreakOnMain)
407
        sp.breakOnMain = true;
hjk's avatar
hjk committed
408

409
    return doCreate(sp, runConfiguration, errorMessage);
410
411
}

412
static bool fixupEngineTypes(DebuggerStartParameters &sp, RunConfiguration *rc, QString *errorMessage)
hjk's avatar
hjk committed
413
414
{
    if (sp.masterEngineType != NoEngineType)
415
        return true;
hjk's avatar
hjk committed
416
417
418

    if (sp.executable.endsWith(_(".py"))) {
        sp.masterEngineType = PdbEngineType;
419
        return true;
hjk's avatar
hjk committed
420
421
422
    }

    if (rc) {
423
        DebuggerRunConfigurationAspect *aspect
424
                = rc->extraAspect<Debugger::DebuggerRunConfigurationAspect>();
425
        if (const Target *target = rc->target())
426
427
            if (!fillParameters(&sp, target->kit(), errorMessage))
                return false;
428
429
        const bool useCppDebugger = aspect->useCppDebugger() && (sp.languages & CppLanguage);
        const bool useQmlDebugger = aspect->useQmlDebugger() && (sp.languages & QmlLanguage);
hjk's avatar
hjk committed
430
431
432
        if (useQmlDebugger) {
            if (useCppDebugger) {
                sp.masterEngineType = QmlCppEngineType;
433
                sp.firstSlaveEngineType = sp.cppEngineType;
hjk's avatar
hjk committed
434
435
436
437
438
                sp.secondSlaveEngineType = QmlCppEngineType;
            } else {
                sp.masterEngineType = QmlEngineType;
            }
        } else {
439
            sp.masterEngineType = sp.cppEngineType;
hjk's avatar
hjk committed
440
        }
441
        return true;
hjk's avatar
hjk committed
442
    }
443
444
    sp.masterEngineType = sp.cppEngineType;
    return true;
hjk's avatar
hjk committed
445
446
447
}

DebuggerRunControl *DebuggerRunControlFactory::doCreate
448
    (const DebuggerStartParameters &sp0, RunConfiguration *rc, QString *errorMessage)
hjk's avatar
hjk committed
449
{
450
451
    TaskHub::clearTasks(Debugger::Constants::TASK_CATEGORY_DEBUGGER_DEBUGINFO);
    TaskHub::clearTasks(Debugger::Constants::TASK_CATEGORY_DEBUGGER_RUNTIME);
452

hjk's avatar
hjk committed
453
454
455
    DebuggerStartParameters sp = sp0;
    if (!debuggerCore()->boolSetting(AutoEnrichParameters)) {
        const QString sysroot = sp.sysRoot;
456
        if (sp.debugInfoLocation.isEmpty())
hjk's avatar
hjk committed
457
458
459
460
461
462
463
            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"));
        }
464
465
    }

466
    if (!fixupEngineTypes(sp, rc, errorMessage))
hjk's avatar
hjk committed
467
468
469
470
471
        return 0;

    return new DebuggerRunControl(rc, sp);
}

472
473
474
475
476
IRunConfigurationAspect *DebuggerRunControlFactory::createRunConfigurationAspect(RunConfiguration *rc)
{
    return new DebuggerRunConfigurationAspect(rc);
}

hjk's avatar
hjk committed
477
478
479
DebuggerRunControl *DebuggerRunControlFactory::createAndScheduleRun
    (const DebuggerStartParameters &sp, RunConfiguration *runConfiguration)
{
480
    QString errorMessage;
481
482
483
    if (runConfiguration && !runConfiguration->ensureConfigured(&errorMessage))
        ProjectExplorer::ProjectExplorerPlugin::showRunErrorMessage(errorMessage);

484
485
486
    DebuggerRunControl *rc = doCreate(sp, runConfiguration, &errorMessage);
    if (!rc) {
        ProjectExplorer::ProjectExplorerPlugin::showRunErrorMessage(errorMessage);
hjk's avatar
hjk committed
487
        return 0;
488
    }
hjk's avatar
hjk committed
489
490
491
492
493
    debuggerCore()->showMessage(sp.startMessage, 0);
    ProjectExplorerPlugin::instance()->startRunControl(rc, DebugRunMode);
    return rc;
}

494
495
DebuggerEngine *DebuggerRunControlFactory::createEngine(DebuggerEngineType et,
    const DebuggerStartParameters &sp, QString *errorMessage)
496
497
498
{
    switch (et) {
    case GdbEngineType:
499
        return createGdbEngine(sp);
500
    case CdbEngineType:
501
        return createCdbEngine(sp, errorMessage);
502
503
504
    case PdbEngineType:
        return createPdbEngine(sp);
    case QmlEngineType:
505
        return createQmlEngine(sp);
506
507
    case LldbEngineType:
        return createLldbEngine(sp);
hjk's avatar
hjk committed
508
509
    case QmlCppEngineType:
        return createQmlCppEngine(sp, errorMessage);
510
511
    default:
        break;
512
    }
hjk's avatar
hjk committed
513
    *errorMessage = DebuggerPlugin::tr("Unable to create a debugger engine of the type '%1'").
514
                    arg(_(engineTypeName(et)));
515
516
517
    return 0;
}

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